Sort The Array | Practice | GeeksforGeeks

Given a random set of numbers, Print them in sorted order.
Example 1:
Input:
N = 4
arr[] = {1, 5, 3, 2}
Output: {1, 2, 3, 5}
Explanation: After sorting array will 
be like {1, 2, 3, 5}.
Example 2:
Input:
N = 2
arr[] = {3, 1}
Output: {1, 3}
Explanation: After sorting array will
be like {3, 1}.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function sortArr() which takes the list of integers and the size N as inputs and returns the modified list.

Expected Time Complexity: O(N * log N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, A[i] ≤ 105
0
shubhamshah141022 days ago
I Have used Merge sort in java but it gives me a runtime error for time being exceeded. How can this code be optimized??class Solution {
   int[] sortArr(int[] arr, int n)    {             sort(arr, 0, n-1);     return arr;   }   public static void sort(int a[], int low, int high)   {        int[] temp= new int[a.length];       if(low<high)       {           int mid=low+ (high-low)/2;           sort(a,low,mid);           sort(a,mid+1,high);           merge(a,temp,low, mid, high);       }   }   public static void merge(int[] a ,int[] temp, int low, int mid , int high)   {       for(int i=low;i<=high;i++)       {           temp[i]=a[i];       }       int i=low,j=mid+1, k=low;       while(i<=mid&&j<=high)       {           if(temp[i]<=temp[j])           {               a[k]=temp[i];               i++;           }           else           {               a[k]=temp[j];               j++;           }           k++;       }       while(i<=mid)       {           a[k]=temp[i];               i++;               k++;       }          }} 
0
jy9041471 week ago
 int[] sortArr(int[] arr, int n)    {       sort(arr,0,arr.length-1);       return arr;          }    public static void merge(int arr[], int l, int m, int r)       {           int n1 = m -l+ 1;           int n2 = r - m;           int L[] = new int[n1];           int R[] = new int[n2];           for (int i = 0; i < n1; i++)               L[i] = arr[l + i];           for (int j = 0; j < n2; j++)               R[j] = arr[m + 1 + j];           int i = 0, j = 0;           int k = l;           while (i < n1 && j < n2) {               if (L[i] <= R[j]) {                   arr[k] = L[i];                   i++;               }               else {                   arr[k] = R[j];                   j++;               }               k++;           }           while (i < n1) {               arr[k] = L[i];               i++;               k++;           }           while (j < n2) {               arr[k] = R[j];               j++;               k++;           }       }     public   static void sort(int arr[], int l, int r)       {           if (l < r) {               int m =(l+ r)/2;               sort(arr, l, m);               sort(arr, m + 1, r);               merge(arr, l, m, r);
           }
       }
0
nandasobhan4 weeks ago
 vector<int> sortArr(vector<int>arr, int n){   //complete the function here   sort(arr.begin(),arr.end());   return arr;   }
0
shreyash9779664 weeks ago
vector<int> sortArr(vector<int>a, int n){
    //complete the function here
    sort(a.begin(),a.end());
    return a;
    }
0
patelshivanshu20171 month ago
//java solution
//merge sort for nlogn time complexity
 
class Solution {                void merge(int[]arr,int s,int m,int l){           int i,j,k;           int n1=m+1-s;           int n2=l-m;                      int []leftarr=new int[n1];           int []rightarr=new int[n2];                      for(i=0;i<n1;i++){               leftarr[i]=arr[s+i];           }           for(j=0;j<n2;j++){               rightarr[j]=arr[m+1+j];           }           i=0;           j=0;           k=s;           while(i<n1 && j<n2){               if(leftarr[i]<=rightarr[j]){                   arr[k]=leftarr[i];                   i++;               }else{                   arr[k]=rightarr[j];                   j++;               }               k++;           }           while(i<n1){               arr[k]= leftarr[i];               i++;               k++;           }           while(j<n2){               arr[k]=rightarr[j];               j++;               k++;           }       }       void sort(int arr[],int s,int l){           if(s<l){               int m=(s+l)/2;               sort(arr,s,m);               sort(arr,m+1,l);               merge(arr,s,m,l);           }       }        int[] sortArr(int[] arr, int n)    {        // code here       sort(arr,0,arr.length-1);       return arr;   }       } 
0
tushargoutam151 month ago
Python3 1 line Solution:
arr.sort()  this will sort the arr in O(log n)
now just return the arr
code:
class Solution:   def sortArr(self, arr, n):        arr.sort()       return arr
0
devil041 month ago
java solution(100% correct)
Arrays.sort(arr);  return arr;
0
popudev071 month ago
typical mergeSort TC O(nlogn) but SPace took O(n) 
//ideal method would be quicksort
 
private static void mergeSort(int[] arr, int start, int end) { if (start < end) {  int mid = (start + end) / 2;  mergeSort(arr, start, mid);  mergeSort(arr, mid + 1, end);  merge(arr, start, mid, end); }}
private static void merge(int[] arr, int start, int mid, int end) { if (arr[mid] > arr[mid + 1]) {  int[] temp = new int[end - start + 1];  int i = start, j = mid + 1, k = 0;
  while (i <= mid && j <= end) {   if (arr[i] < arr[j]) {    temp[k++] = arr[i++];   } else {    temp[k++] = arr[j++];   }  }
  while (i <= mid) {   temp[k++] = arr[i++];  }
  while (j <= end) {   temp[k++] = arr[j++];  }  k = 0;  for (i = start; i < end + 1; i++) {   arr[i] = temp[k++];  } }}
+1
atulp2001np1 month ago
	vector<int >v;
    
    priority_queue<int>maxh;
    for (int i=0; i<n; i++){
        maxh.push(arr[i]);
        
    }
    while(maxh.size()!=0){
        v.push_back(maxh.top());
        maxh.pop();
    }
    reverse(v.begin(),v.end());
    
    return v;
-1
jaisprajwal19992 months ago
So here is a one-liner for java 
class Solution 
{ 
    int[] sortArr(int[] arr, int n) 
    { 
        Arrays.sort(arr);
        return arr;
    }
} 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.