









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of various sorting algorithms, including comparison sort, bubble sort, selection sort, tree sort, heap sort, quick sort, merge sort, linear sort, counting sort, and radix sort. Each algorithm is explained in detail, including its approach, performance, and example. The document also includes code snippets for some of the algorithms.
Typology: Study notes
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Bubble sort Selection sort Tree sort Heap sort Quick sort Merge sort
Counting sort Bucket (bin) sort Radix sort
O(n^2 )
O(n log(n) )
O(n)
Sorting
Arrange elements in predetermined order Based on key for each element Derived from ability to compare two keys by size
Stable ⇒ relative order of equal keys unchanged In-place ⇒ uses only constant additional space External ⇒ can efficiently sort large # of keys
Sorting
Only uses pairwise key comparisons Proven lower bound of O( n log(n) )
Uses additional properties of keys
Bubble Sort Code
void bubbleSort(int[] a) { int outer, inner; for (outer = a.length - 1; outer > 0; outer--) { for (inner = 0; inner < outer; inner++) { if (a[inner] > a[inner + 1]) { int temp = a[inner]; a[inner] = a[inner + 1]; a[inner + 1] = temp; } } } }
Sweep through array
Swap with right neighbor if larger
Selection Sort
O( n^2 ) average / worst case
7 2 8 5 4
2 7 8 5 4
2 4 8 5 7
2 4 5 8 7
2 4 5 7 8
Selection Sort Code
void selectionSort(int[] a) { int outer, inner, min; for (outer = 0; outer < a.length - 1; outer++) { min = outer; for (inner = outer + 1; inner < a.length; inner++) { if (a[inner] < a[min]) { min = inner; } } int temp = a[outer]; a[outer] = a[min]; a[min] = temp; } }
Swap with smallest element found
Sweep through array
Find smallest element
Tree Sort
Binary search tree O( n log(n) ) average case O( n^2 ) worst case Balanced binary search tree O( n log(n) ) average / worst case
Binary search tree
Quick Sort Algorithm
Sort w/ other algorithm
L elements < x E elements = x G elements > x
If not sorting in place
x
x
x
Quick Sort Code
void quickSort(int[] a, int x, int y) { int pivotIndex; if ((y – x) > 0) { pivotIndex = partionList(a, x, y); quickSort(a, x, pivotIndex – 1); quickSort(a, pivotIndex+1, y); }
}
int partionList(int[] a, int x, int y) {
… // partitions list and returns index of pivot
}
Lower end of array region to be sorted
Upper end of array region to be sorted
Quick Sort Example
Partition & Sort Result
Quick Sort Code
int partitionList(int[] a, int x, int y) { int pivot = a[x]; int left = x; int right = y; while (left < right) { while ((a[left] < pivot) && (left < right)) left++; while (a[right] > pivot) right--; if (left < right) swap(a, left, right); } swap(a, x, right); return right; }
Use first element as pivot
Partition elements in array relative to value of pivot
Place pivot in middle of partitioned array, return index of pivot
Merge Sort Example
Split Merge
Merge Sort Code
void mergeSort(int[] a, int x, int y) { int mid = (x + y) / 2; if (y == x) return; mergeSort(a, x, mid); mergeSort(a, mid+1, y); merge(a, x, y, mid); }
void merge(int[] a, int x, int y, int mid) { … // merges 2 adjacent sorted lists in array }
Lower end of array region to be sorted
Upper end of array region to be sorted
Merge Sort Code
void merge (int[] a, int x, int y, int mid) {
int size = y – x; int left = x; int right = mid+1; int[] tmp; int j; for (j = 0; j < size; j++) { if (left > mid) tmp[j] = a[right++]; else if (right > y) || (a[left] < a[right]) tmp[j] = a[left++]; else tmp[j] = a[right++]; } for (j = 0; j < size; j++) a[x+j] = tmp[j]; }
Lower end of 1 st^ array region
Upper end of 2 nd^ array region
Upper end of 1 st^ array region
Copy merged array back
Copy smaller of two elements at head of 2 array regions to tmp buffer, then move on
Counting Sort
O( n + k ) average / worst case
Counting Sort Code
void countSort(int[] a, int k) { // keys have value 0…k
int[] b; int[] c; int i; for (i = 0; i ≤ k; i++) // initialize counts c[i] = 0; for (i = 0; i < a.size(); i++) // count # keys c[a[i]]++; for (i = 1; i ≤ k; i++) // calculate # keys ≤ value i c[i] = c[i] + c[i-1] for (i = a.size()-1; i > 0; i--) { b[c[a[i]]-1] = a[i]; // move key to location c[a[i]]--; // decrement # keys ≤ a[i] } for (i = 0; i < a.size(); i++) // copy sorted list back to a a[i] = b[i];
}
Bucket (Bin) Sort
Pick large k so can sort n / k elements in O(1) time O( n ) average case O( n^2 ) worst case If most elements placed in same bucket and sorting buckets with O( n^2 ) algorithm
Bucket Sort Example
Radix Sort
O( d × (n+k) ) ≈ O(n) average / worst case
Sorting Summary