Docsity

Notes for Sorting Algorithms and Overview | CMSC 132, Study notes of Computer Science

Partial preview of the text

Download Notes for Sorting Algorithms and Overview | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

Sorting Algorithms

CMSC 132

Department of Computer Science

University of Maryland, College Park

Overview

Comparison sort

Bubble sort Selection sort Tree sort Heap sort Quick sort Merge sort

Linear sort

Counting sort Bucket (bin) sort Radix sort

O(n

2

O(n log(n) )

O(n)

Sorting

Comparison sort

Only uses pairwise key comparisons Proven lower bound of O( n log(n) )

Linear sort

Uses additional properties of keys

Bubble Sort

Approach

Iteratively sweep through shrinking portions of list Swap element x with its right neighbor if x is larger

Performance

O( n

2

) average / worst case

Bubble Sort Code

void bubbleSort(int[] a) {

for (int outer = a.length - 1; outer > 0; outer--)

for (int inner = 0; inner < outer; inner++)

if (a[inner] > a[inner + 1])

swap(a, inner, inner+1);

Sweep

through

array

Swap with

right neighbor

if larger

Example

Selection Sort

Approach

Iteratively sweep throughshrinking portions of list Select largest elementfound in each sweep Swap largest element withend of current list

Performance

O( n2 ) average / worstcase

Example

Binary search tree

Tree Sort

Approach

Insert elements in binarysearch tree List elements using inordertraversal

Performance

Binary search tree

O( n log(n) ) average case O( n2 ) worst case

Balanced binary search tree

O( n log(n) ) average /worst case

Example

Heap

Heap Sort

Approach

Insert elements in heap Remove smallest elementin heap, repeat List elements in order ofremoval from heap

Performance

O( n log(n) ) average /worst case

x

x

L

G

E

x

Quick Sort Algorithm

If list below size K

Sort w/ other algorithm

Else pick pivot x andpartition S into

L elements <= x E pivot element x G elements > x

Quicksort L & G Concatenate L, E & G

If not sorting in place

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

Lowerend of

array

region

to be

sorted

Upper end of

array

region

to be

sorted

Quick Sort Code

static int partitionList(int[] a, int x, int y) {

int left = x+1;int right = y;int pivot = a[x];while (true) {

while (a[left] <= pivot && left < right)

left++;

while (a[right] > pivot)

right--;

if (left >= right) break;swap(a, left, right);

} swap(a, x, right);return right;

}

Use first

elementas pivot

Partition elements

in array relative to

value of pivot

Place pivot in middle

of partitioned array,

return index of pivot

Quick Sort Code

static int partitionList(int a[], int first, int last) {

int i, pivot, border;pivot = a[first];border = first;for (i = first + 1; i <= last; i++) {

if (a[i] <= pivot) {

border++;swap(a, border, i);

}

} swap(a, first, border);return border;

} static void swap(int a[], int x, int y) {

int temp = a[x];a[x] = a[y];a[y] = temp;

}

Merge Example

Merge Sort Example

Split

Merge