2B Sorts

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Sorting

Algorithms
Unit 2
CPRG304 – Object-Oriented
Programming III

Kitty Wong © 2023


Sorting
• This is what our software has to do most of the time!
• Sort then we can search faster!
• E.g. Binary searching

• Given an unsorted array of data, rearrange the data


into a certain order (e.g. ascending order)
• If our data doesn’t change then it doesn’t matter how we sort
the data
• If we allow adds, removes, or updates then we need to make
sure it’s always in sorted order
• Must be efficient – always running!
Sorting Algorithms
• So many to choose from!

• Internal Sorting
• The whole collection of data resides in memory (RAM)
• Faster access = faster sorting
• External Sorting
• Data resides in both memory and secondary storage (hard
disk)
• Slower due to transfer of data to/from disk
• We will not cover these types of sorts in this course
Insertion Sort
• Successively read each value and insert it into its
correct position in an array
• Every much like you would sort a hand of cards!

for (int i = 1; i < list.length; i++) {


insert list[i] into a sorted sublist list[0..i-
1] so that list[0..i] is sorted.
}
Visualization of Insertion Sort

• https://www.youtube.com/watch?v=DFG-XuyPYUQ&ab_channel=CS50
Complexity of Insertion Sort
• Time complexity – O(n2)
• Nested loop
• Outer loop is the element to be sorted in the list
• Inner loop iterates through sub-list to find the current element’s place

• Space complexity – O(1)


Selection Sort
• Find the smallest value in the array, swap it into the
leftmost element where it belongs, then forget this
element, repeat with next smallest value until done

for (int i = 0; i < list.length - 1; i++) {


select the smallest element in list[i..list.length-
1];
swap the smallest with list[i], if necessary;
// list[i] is now in its correct position.
// The next iteration applies on
list[i+1..list.length-1]
}
Visualization of Selection Sort

• https://www.youtube.com/watch?v=f8hXR_Hvybo&ab_channel=CS50
Complexity of Selection Sort
• Time complexity – O(n2)
• Nested loop
• Outer loop iterated to find smallest element of in the list for the pass
• Inner loop iterated to does the comparision and swap

• Space complexity – O(1)


• Sorting in place, no extra space required
Bubble Sort
• Read each value and compare it to the next value
• If the current value is > than the next, swap the values
• Repeat until end of collection
• This is the first pass which moves the correct element to the last position
• Start again at the beginning of the collection, repeat the comparisons and
swapping if necessary
• Sorted when you pass through the entire collection without swapping any values

for (int k = 1; k < list.length; k++) {


// Perform the kth pass
for (int i = 0; i < list.length - k; i++) {
if (list[i] > list[i + 1]) 5 swap list[i] with list[i +
1];
}
}
Visualization of Bubble Sort

• https://www.hackerearth.com/practice/algorithms/sorting/bubble-sort/visualiz
e/
Complexity of Bubble Sort
• Time complexity – O(n2)
• Nested loops
• Outer loop to determine do a pass for all n elements in the list
• Inner loop to compare elements and swap the largest ones
• The first iteration of the inner loop will iterate through all n elements

• Space complexity – O(1)


• Sorting in place, no extra space required
Divide and Conquer
• To break the problem into several subproblems that
are similar to the original problem, but smaller in size

1. Divide the problem into a number of subproblems


2. Conquer the subproblems by solving them recursively
• If the subproblem sizes are small enough, just solve the
subproblem in a straightforward manner

3. Combine the solutions to the subproblems into the


solution for the original problem
• Perhaps the most overlooked step!
Merge Sort
• Merge Sort
1. Divide: the array into two subarrays of about equal length
2. Conquer: Sort the subarrays separately using merge-sort
3. Combine: Merge the sorted subarrays

• Merging for Merge Sort


• Given two sorted arrays, make a third sorted array containing copies of all components of the
two original two arrays
• We will call merge sort recursively to sort an unsorted array

public static void mergeSort(int[] list) {


if (list.length > 1) {
mergeSort(list[0 ... list.length / 2]);
mergeSort(list[list.length / 2 + 1 ... list.length]);
merge list[0 ... list.length / 2] with 6 list[list.length / 2 +
1 ... list.length];
}
}
Visualization of Merge Sort
Merging for Merge Sort:

• https://www.youtube.com/watch?v=EeQ8pwjQxTM&ab_channel=CS50
Time Complexity of Merge Sort
• Merge Sort (combination)
• Time Complexity: O(n log n)
• Space Complexity: O(n)

• Merging for Merge Sort


• Time Complexity: O(n)
• Space Complexity: O(2n)
• Not considering variations of the algorithm
Quicksort
• Choose any value from the array called the pivot)
• Partition the array into three subarrays such that:
• The left subarray contains only values less than (or equal to) the pivot
• The middle subarray contains only the pivot
• The right subarray contains only values greater than the pivot
• Repeat with the left subarray and the right subarray separately until sorted

public static void quickSort(int[] list) {


if (list.length > 1) {
select a pivot;
partition list into list1 and list2 such that all
elements in list1 <= pivot and 6 all elements in
list2 > pivot;
quickSort(list1);
quickSort(list2);
}
}
Visualization of Quicksort

• Coding with John


Complexity of Quicksort
• Time Complexity: O(n2) or O(n log n)
• At worst n comparisons and n swaps per partition
• O(n) partition time
• Pivot at worst divides array into a large subarray and an
empty one
• O(n2) worst case

• Space Complexity: O(n) or O(log n)


• Depending on how the pivot divides the array
Selecting a Pivot
• Selecting a pivot wisely can significantly improve the performance
of the algorithm

• First element:
• Simple, no overhead
• Still O(n2) if collection is sorted

• Random selection:
• Safe in almost all situations, but calculating a random number can be costly

• Median element:
• Optimal, but impractical to implement
Median-of-3
• Value of the median element out the elements in first
position, middle position and last position
• If collection is not odd-number sized, just pick the element
before or after
0 1 2 3 4 5 6 7 8 9
a
• E.g. sort a[0], a[4], a[9], pivot = middle value
• Easy and fast
• A good compromise to finding the actual median
• Works even if the collection is already sorted
Non-Comparative Sorts
• Comparative sorts can perform at best O(n log n)
• O(log n) comparisons and O(n) merge, shifts or swaps

• Non-comparative = no comparisons
• No more O(log n) for comparing elements!
• Also know distribution sorts

• Only works for certain types of data

• Can be external (i.e. all data does not need to be in


memory)
Radix Sort
• Using multiple passes, distribute each item to a bucket
according to part of the item’s key beginning with the
least significant part of the key
• After each pass, items are collected from the buckets, keeping
the items in order, then redistributed according to the next
most significant part of the key

• Can only be used with data within a specific set


• Digits 0-9, alphabet A-Z, of a particular length
• E.g. postal code – T3K0C3
• E.g. card - A<2<3<4<5<6<7<8<9<10<J<Q<K,  <  <  < 
Radix Sort Example
• Data: 493, 812, 715, 710, 195, 437, 582, 340, 385
• Start with the least significant digit (the one’s position)
• Gather the same values and put them together in a bucket
• Usually implemented as a list

• “Drain” the buckets in order:


• We end up with: 710, 340, 812, 582, 493, 715, 195, 385, 437
Radix Sort Example
• Data: 710, 340, 812, 582, 493, 715, 195, 385, 437
• Now to the next least significant digit (the ten’s position)
• Gather the same values and put them together in a bucket

• “Drain” the buckets in order again:


• We end up with: 710, 812, 715, 437, 340, 582, 385, 493, 195
Radix Sort Example
• Data: 710, 812, 715, 437, 340, 582, 385, 493, 195
• Now to the last digit (the hundred’s position)
• Gather the same values and put them together in a bucket

• “Drain” the buckets in order again:


• We now have our sorted data:
195, 340, 385, 437, 493, 582, 710, 715, 812
Radix Sort
• Time complexity: O(n)
• Space complexity: O(n)
• can be optimized depending on data set

• Implementation:
References
• Data Structures and Abstractions with Java, 5th edition,
2019, Frank M. Carrano, Pearson.
• Big Java: Early Objects, 7th edition, 2018, Cay S.
Horstmann, Wiley.

• All content are ©2021 Southern Alberta Institute of


Technology unless otherwise indicated.

You might also like