
























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
Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























Overview
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
Only uses pairwise key comparisons Proven lower bound of O( n log(n) )
Uses additional properties of keys
Bubble Sort
Iteratively sweep through shrinking portions of list Swap element x with its right neighbor if x is larger
O( n
2
) average / worst case
Bubble Sort Code
Sweep
through
array
Swap with
right neighbor
if larger
Selection Sort
Iteratively sweep throughshrinking portions of list Select largest elementfound in each sweep Swap largest element withend of current list
O( n2 ) average / worstcase
Binary search tree
Tree Sort
Insert elements in binarysearch tree List elements using inordertraversal
Binary search tree
O( n log(n) ) average case O( n2 ) worst case
Balanced binary search tree
O( n log(n) ) average /worst case
Heap
Heap Sort
Insert elements in heap Remove smallest elementin heap, repeat List elements in order ofremoval from heap
O( n log(n) ) average /worst case
x
x
x
Quick Sort Algorithm
Sort w/ other algorithm
L elements <= x E pivot element x G elements > x
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