Sorting Algorithms
Probably the most frequent operation computers do.
Sorting
18 - 2
Definition of Sorting
3 | 5 | 12 | 19 | 50 | 52 | 60 | 60 | 85 | 91 | 92 | 93 | 98 |
91 | 50 | 12 | 60 | 5 | 52 | 3 | 19 | 85 | 60 | 98 | 93 | 92 |
Example of sorting client
public class SortPhoneList
{
public static void main(String[] args)
{
Contact[] friends = new Contact[7];
friends[0] = new Contact("John", "Smith", "610-555-7384");
friends[1] = new Contact("Sarah", "Barnes", "215-555-3827");
friends[2] = new Contact("Mark", "Riley", "733-555-2969");
friends[3] = new Contact("Laura", "Getz", "663-555-3984");
friends[4] = new Contact("Larry", "Smith", "464-555-3489");
friends[5] = new Contact("Frank", "Phelps", "322-555-2284");
friends[6] = new Contact("Marsha", "Grant", "243-555-2837");
Sorting.selectionSort(friends);
for (int i = 0; i < friends.length; i++)
System.out.println(friends[i]);
}
}
18 - 4
Example of data to sort
/**
* Contact represents a phone contact.
* @author Java Foundations
*/
public class Contact implements Comparable<Contact> {
private String firstName, lastName, phone;
/**
* Sets up this contact with the specified information.
*
* @param first a string representation of a first name
* @param last a string representation of a last name
* @param telephone a string representation of a phone number
*/
public Contact(String first, String last, String telephone)
{
firstName = first;
lastName = last;
phone = telephone;
}
18 - 5
Example on value to sort on
/**
* Uses both last and first names to determine lexical ordering.
*
* @param other the contact to be compared to this contact
* @return the integer result of the comparison
*/
public int compareTo(Contact other)
{
int result;
if (lastName.equals(other.lastName))
result = firstName.compareTo(other.firstName);
else
result = lastName.compareTo(other.lastName);
return result;
}
}
18 - 6
Selection Sort
Select the next element to place in its proper position
Sorting using selectionSort
SelectionSort orders values by repeatedly �putting a particular value into its final sorted position
The algorithm:
Visualization https://visualgo.net/sorting
Practice!
selectionSort( )
public static void selectionSort(Comparable[] data) {
int min;
for(int index = 0; index < data.length – 1; index++){
min = index;
for(int scan = index+1; scan < data.length; scan++)
if (data[scan].compareTo(data[min]) < 0)
min = scan;
swap(data, min, index);
}
} // What is its running time?
Insertion Sort
Given a partially sorted set of elements,�Insert the next element in its proper position
Sorting using insertionSort
InsertionSort orders a list of values by repeatedly �inserting a particular value into a sorted subset of the list
The algorithm:
Sorting using insertionSort
InsertionSort orders a list of values by repeatedly �inserting a particular value into a sorted subset of the list
The algorithm:
3 | 9 | 6 | 1 | 2 |
3 | 9 | 6 | 1 | 2 |
3 | 6 | 9 | 1 | 2 |
1 | 3 | 6 | 9 | 2 |
1 | 2 | 3 | 6 | 9 |
(3) is sorted sublist. Consider 9.
Shift nothing, insert 9. (3, 9) are sorted. Consider 6.
Shift 9, insert 6. (3, 6, 9) are sorted. Consider 1.
Shift 3, 6, 9, insert 1. (1, 3, 6, 9) sorted. Consider 2.
Shift 3, 6, 9, insert 2. (1, 2, 3, 6, 9) sorted.
Practice!
insertionSort( )
public static void insertionSort(Comparable[] data) {
for(int index = 1; index < data.length; index++) {
Comparable key = data[index];
int position = index;
while (position > 0 && data[position-1].compareTo(key) > 0){
// shift larger values to the right
data[position] = data[position-1];
position--;
}
data[position] = key;
}
} //What is its running time?
Merge Sort
Given two partially sorted sets of elements,�Merge them into a new sorted set
Sorting using mergeSort
Merge sort orders a list of values by recursively �dividing the list in half until each sublist has one element, then recombining (merging) the sublists
The algorithm:
Split: Divide the list into two equal parts—left and right. If the list has odd length, one half will have an extra element.
Sort: Recursively use merge-sort to sort the two lists. Note that by definition, a list of one element is sorted (base case).�
Merge: Merge the two sorted parts into one sorted list
Merge sort – Split
90 65 7 305 120 110 8
65 7
305 120 110 8
90
65
7
90
305 120
110 8
305
120
110
8
90 65 7
Merge sort – Sort & Merge
7 8 65 90 110 120 305
7 65
90
120 305
8 110
65
7
90
305
120
110
8
8 110 120 305
7 65 90
mergeSort( )
public static void mergeSort(Comparable[] data,
int min, int max){
if (min < max) {
int mid = (min + max) / 2;
mergeSort(data, min, mid);
mergeSort(data, mid+1, max);
merge(data, min, mid, max);
}
}
Note: This is NOT the exact code executing in the visualization of the previous slide
merge (1/2)
public static void merge(Comparable[] data,
int first, int mid, int last) {
Comparable[] temp = new Comparable[data.length];
int first1 = first, last1 = mid; //ends of 1st subarray
int first2 = mid+1, last2 = last; //ends of 2nd subarray
int index = first1; // destination in temp array
// copy smaller item from each subarray into temp � // until one of the subarrays is exhausted
while(first1 <= last1 && first2 <= last2){
if(data[first1].compareTo(data[first2]) < 0) {
temp[index] = data[first1];
first1++;
} else {
temp[index] = data[first2];
first2++;
}
index++;
}
merge (2/2)
//copy remaining elements from first subarray, if any
while (first1 <= last1){
temp[index] = data[first1];
first1++;
index++;
}
//copy remaining elements from second subarray, if any
while (first2 <= last2){
temp[index] = data[first2];
first2++;
index++;
}
//copy merged data into original array
for(index = first; index <= last; index++){
data[index] = temp[index];
}
}
Practice!
Note: for this exercise, do this by carefully stepping through the merge sort code.
Sort Efficiencies
Algorithm | Best | Average | Worst | Space in addition to original array |
Insertion | O(n) | O(n^2) | O(n^2) | O(1) |
Selection | O(n^2) | O(n^2) | O(n^2) | O(1) |
Merge | O(n log n) | O(n log n) | O(n log n) | O(n) |
Sort Efficiencies
Bonus: Quick Sort
Select an element at random and �place it in its proper location, with �smaller elements to its left and �larger elements to its right
Sorting using quickSort
Example of Quicksort
Input array
First element becomes the pivot
Pivot is placed to its final position during partition
Two sub-arrays are sorted recursively
First elements become pivots
Pivots are placed in final position
Four sub-arrays are sorted recursively…
65
7
90
305
120
110
8
65
7
90
305
120
110
8
65
7
8
90
120
110
305
65
7
8
90
120
110
305
65
7
8
90
120
110
305
65
7
8
90
120
110
305
65
7
8
90
120
110
305
quicksort( )
public static void quickSort� (Comparable[] data, int min, int max) {
int pivot;
if(min < max){
pivot = partition(data, min, max); //make partitions
quickSort(data, min, pivot-1); //sort left part
quickSort(data, pivot+1, max); //sort right part
}
}
Example of partition
Partition finished. After that, the two sub-arrays are sorted recursively
65
7
90
305
120
110
8
65
7
90
305
120
110
8
65
7
90
8
120
110
305
65
7
8
90
120
110
305
partition
private static int partition(Compareable[] data, int min, int max) {
Comparable pivotValue = data[min];
int left = min; int right = max;
while(left < right){
//search for an element that is > pivotValue
while(data[left].compareTo(pivotValue) <= 0 && left < right)
left++;
//search for an element that is < pivotValue
while(data[right].compareTo(pivotValue) > 0)
right--;
if(left < right)
swap(data, left, right); // put them in the other part
}
//move the pivotElement to its final position
swap(data, min, right);
return right; // caller needs to know location of split
}
Practice!
Differences; Pros and Cons