Sorting

Sorting is a technique to re-order the data in increasing or decreasing order.

Data need not be integers; it can be strings, lists or anything which can have comparable objects.

Each programming language comes with an in-built Sort function.
In C, you can use quick sort as follows

void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))  

here

  • *base is pointer to the first element.
  • nitems is the number of items to sort.
  • size is the size of each element.
  • compar is a compare function which can return -1 for less, 0 for equal and +1 for greater.

Sample Code in C

#include <stdio.h>
#include <stdlib.h>

int values[] = { 88, 56, 100, 2, 25 };

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

int main()
{
   int n;

   printf("Before sorting the list is: \n");
   for( n = 0 ; n < 5; n++ ) 
   {
      printf("%d ", values[n]);
   }

   qsort(values, 5, sizeof(int), cmpfunc);

   printf("\nAfter sorting the list is: \n");
   for( n = 0 ; n < 5; n++ ) 
   {   
      printf("%d ", values[n]);
   }

   return(0);
}

In C++ algorithm library comes with built in sort function.

example using array and a vector

#include <bits/stdc++.h>
using namespace std; 

int main() {
    int a[] = {5,4,3,2,1};
    vector <int> b(a,a+5); //initialised b using elements of a. 
    sort(a,a+5);
    sort(b.begin(),b.end());
    for (auto i :a) {
        cout<<i<<" ";
    }
    cout<<endl;
    for (auto i:b) {
        cout<<i<<" ";
    }
    cout<<endl;
}

Note that you can also give your custom compare function in the third argument.
In order to reverse sort you can do

sort(b.rbegin(),b.rend());

In Python you can sort using

a.sort()  

sometimes you can even define key which is like compare function.

arr = [[3, 4, 2, 1], [2, 1, 3, 4]]
arr.sort(key = lambda x : x[1]) #key should be a callable function 
#here lambda is a returns the 1st element inside the list.  
#arr is now [[2, 1, 3, 4], [3, 4, 2, 1]], sorted on 1st index.  

Sample Code in Java :

import java.util.* ;
public class sorting{
    public static void main(String []args){
        Scanner sc = new Scanner(System.in) ;
        int n ;
        n = sc.nextInt() ;
        int A[] = new int[n] ;
        for(int i=0;i<n;i++)
            A[i] = sc.nextInt() ;
        Arrays.sort(A) ;
        for(int i=0;i<n;i++)
            System.out.print(A[i] + " ") ;
        System.out.print("\n") ;
    }
}
 
Related challenge for Sorting
Go to Top

Manhattan Distance

Manhattan distance between two points is the sum of the absolute differences of their Cartesian coordinates.
For a space. Distance between and is given by :

Consider a space. The Manhattan distance is given by

 
Go to Top
  1. Challenge Walkthrough
    Let's walk through this sample challenge and explore the features of the code editor.1 of 6
  2. Review the problem statement
    Each challenge has a problem statement that includes sample inputs and outputs. Some challenges include additional information to help you out.2 of 6
  3. Choose a language
    Select the language you wish to use to solve this challenge.3 of 6
  4. Enter your code
    Code your solution in our custom editor or code in your own environment and upload your solution as a file.4 of 6
  5. Test your code
    You can compile your code and test it for errors and accuracy before submitting.5 of 6
  6. Submit to see results
    When you're ready, submit your solution! Remember, you can go back and refine your code anytime.6 of 6
  1. Check your score