Comparison Based Sorting

| Comments

Merge Sort

Merge sort follows divide-and-conquer approach.





Merge-Sort
 1
 2MERGE-SORT(A, p, r) // A is array of numbers
 3{                   // p is starting index of array
 4                    // r is last index of array
 5    if p < r
 6    then q <- (p + r) / 2;  
 7        MERGE-SORT(A, p, q)
 8        MERGE-SORT(A, q + 1, r)
 9        MERGE(A, p, q, r)
10}


Merge Sort

Merge-Sort
 1
 2MERGE(A, p, q, r)
 3n1 <- q - p + 1
 4n2 <- r - q
 5create arrays L[1...n1 + 1] and R[1... n2 + 1] // It took O(n) space
 6
 7//Assign elements to new sub arrays
 8for i <- 1 to n1
 9    do L[i] <- A[p + i - 1]
10     
11for j <- 1 to n2
12    do R[j] <- A[q + j]
13    
14//Mark end of array
15L[n1 + 1] <- 
16R[n2 + 1] <- 


Merging two arrays L[] and R[]

Merge step
 1
 2i <- 1
 3j <- 1
 4
 5for k <- p to r {
 6    do if L[i] <= R[j]
 7        then A[k] <- L[i]
 8            i <- i + 1
 9        else A[k] <- R[j]
10            j <- j + 1
11}


Analysis

Time complexity

Fully expanded recursion tree has logn + 1 levels.
Each level contributes to total cost of cn

Total cost = cn logn + cn = O(nlogn)

Table based comparison

Merge SortHeap SortQuick Sort
Space O(n) In place In place
Best: O(nlogn)
Avg: O(nlogn)
Worst: O(nlogn)
Best: O(nlogn)
Avg: O(nlogn)
Worst: O(nlogn)
Best: O(nlogn)
Avg: O(nlogn)
Worst: O(n2)
Divide and Conquer Divide and Conquer
Runs better than heapsort
in data cache if its on array
Runs faster in small data cache Works really well in
virtual memory env/Caches
Access frequent contiguous
memory locations
Spread throughout the heap Access frequent contiguous
memory locations
Stable Not Stable Not Stable
Parallelize well Do not Parallelize well Parallelize well
Used in external sort Can not be used in external sort.
Locality of ref is issue.
Used in external sort


Merge sort code

Merge Sort Run Code
  1
  2// Merge Sort example
  3// For input array
  4// 2 8 7 1 3 5 6 4 9 0
  5//
  6// First partion in L[] and R[] will be
  7// L[] = 2 8 7 1 3 ==> L[] = 2 8 7 | R[] = 1 3 ==> L[] = 2 8 | R[] = 7
  8// R[] = 5 6 4 9 0
  9//
 10#include <stdio.h>
 11 
 12#define EOA 100000  //End of array
 13 
 14// To enable debug messages uncomment #define
 15// #define DEBUG 1
 16 
 17#ifdef DEBUG
 18#  define D(x) x
 19#else
 20#  define D(x)
 21#endif
 22 
 23void mergesort(int arr[], int p, int r); 
 24void merge(int arr[], int p, int q, int r); 
 25 
 26int arr[10] = {2, 8, 7, 1, 3, 5, 6, 4, 9, 0}; 
 27 
 28int main()
 29{
 30    int i = 0;
 31    printf("Input array\n");
 32    for (i = 0; i <= 9; i++) {
 33        printf("%d ", arr[i]);
 34    }   
 35    printf("\n\n");
 36 
 37    mergesort(arr, 0, 9); 
 38 
 39    printf("Sorted output\n");
 40    for (i = 0; i <= 9; i++) {
 41        printf("%d ", arr[i]);
 42    }   
 43    printf("\n");
 44 
 45    return 0;
 46}
 47 
 48void mergesort(int arr[], int p, int r)
 49{
 50    if (p < r) {
 51        int q = (p + r) / 2;
 52 
 53        mergesort(arr, p, q);
 54        mergesort(arr, q+1, r);
 55        merge(arr, p, q, r);
 56    }
 57}
 58 
 59void merge(int arr[], int p, int q, int r)
 60{
 61    int n1 = q - p + 1;
 62    int n2 = r - q;
 63    int i = 0;
 64    int j = 0;
 65 
 66    int L[15];
 67    int R[15];
 68 
 69    //Copy elements from p to n1 in L[]
 70    for (i = 0; i < n1; i++) {
 71        L[i] = arr[p + i];
 72    }
 73    L[i] = EOA;
 74 
 75    for (j = 0; j < n2; j++) {
 76        R[j] = arr[q + j + 1];
 77    }
 78    R[j] = EOA;
 79 
 80    int lindx = 0;
 81    int rindx = 0;
 82    //Merge array L[] and R[]
 83    for (i = p; i <= r; i++) {
 84        if(L[lindx] <= R[rindx]) {
 85            arr[i] = L[lindx++];
 86        } else {
 87            arr[i] = R[rindx++];
 88        }
 89    }
 90 
 91    // Print debug statements
 92    D(printf("\n######################\n"));
 93    D(printf("Left array\n"));
 94 
 95    for (i = 0; i < n1; i++) {
 96        D(printf("%d ", L[i]));
 97    }
 98 
 99    D(printf("\nRight array\n"));
100    for (i = 0; i < n2; i++) {
101        D(printf("%d ", R[i]));
102    }
103 
104    D(printf("\nAfter Merge\n"));
105    for (i = p; i <= r; i++) {
106        D(printf("%d ", arr[i]));
107    }
108    D(printf("\n\n"));
109}


Quick sort code

Quicksort explanation

Quick Sort Run Code
 1
 2// Quick Sort example
 3// For input array
 4// 2 8 7 1 3 5 6 4
 5//
 6#include <stdio.h>
 7#define IPSIZE 8
 8 
 9// To enable debug messages uncomment #define
10// #define DEBUG 1
11 
12#ifdef DEBUG
13#  define D(x) x
14#else
15#  define D(x)
16#endif
17 
18void quicksort(int arr[], int p, int r); 
19int partition(int arr[], int p, int r); 
20void swap(int i, int j); 
21 
22int arr[IPSIZE] = {2, 8, 7, 1, 3, 5, 6, 4}; 
23 
24int main()
25{
26    int i = 0;
27    printf("Input array\n");
28    for (i = 0; i <= IPSIZE - 1; i++) {
29        printf("%d ", arr[i]);
30    }   
31    printf("\n\n");
32 
33    quicksort(arr, 0, IPSIZE - 1); 
34 
35    printf("Sorted output\n");
36    for (i = 0; i <= IPSIZE - 1; i++) {
37        printf("%d ", arr[i]);
38    }   
39    printf("\n");
40 
41    return 0;
42}
43 
44void quicksort(int arr[], int p, int r)
45{
46    if (p < r) {
47        int pivotIndx = partition(arr, p, r);
48 
49        quicksort(arr, p, pivotIndx - 1);
50        quicksort(arr, pivotIndx + 1, r);
51    }
52}
53 
54int partition(int arr[], int p, int r)
55{
56    int pivot = arr[r];
57    int i = p - 1;
58    int j = 0;
59 
60    // Debug messages
61    D(printf("\n############\n"));
62    D(printf("Partition\n"));
63    D(printf("p=%d, r=%d, pivot=%d\n", p, r, pivot));
64    D(printf("Elements\n"));
65    for (j = p; j <= r; j++) {
66        D(printf("%d ", arr[j]));
67    }
68    D(printf("\n"));
69 
70    for (j = p; j <= r - 1; j++) {
71        if (arr[j] <= pivot) {
72            i++;
73            swap(i, j);
74        }
75    }
76 
77    swap(i + 1, r);
78 
79    D(printf("Elements after partition\n"));
80    for (j = p; j <= r; j++) {
81        D(printf("%d ", arr[j]));
82    }
83    D(printf("\n"));
84 
85    return (i + 1);
86}
87 
88void swap(int i, int j)
89{
90    int temp = arr[i];
91    arr[i] = arr[j];
92    arr[j] = temp;
93}


References

algorithm, sorting

« sorting Recursion »

Comments