Lecture 08 - Sorting Arrays.ppt
Lecture 08 - Sorting Arrays.ppt
Data Structures
2
Today …
Sorting algorithms
3
Learning Objectives
● By the end of this lecture, students must be:
• Implement and use various sorting algorithms
4
1. Introduction
● Sorting also involves traversing a collection
• It is the process of arranging items in some order
• Ascending
• Descending
● E.g.
• Unsorted: { 12, 7, 23, 9 }
• Results of sorting: { 7, 9, 12, 23 }
5
2. Sorting Algorithms
● There are various sorting algorithms
• They all arrange items in some order
● Some are better than others in certain situations
● They have different runtimes
• Some are slow
• Some quick
● We will consider some of these algorithms
● Note:
• The more efficient the sorting algorithm is, the more
complex it will be
a) Bubble Sort
● The simplest sorting algorithm
• Easy to implement
● How does it work?
1. Pass through the entire list (through several passes)
2. Check if adjacent items are out of sync (wrong order)
3. If yes: Swap them
● Inefficient (slow) algorithm
• Loops through all items
• Several times
• Time complexity: O(n2)
Sample method
public void bubbleSort (int arr[ ]) {// assume: ascending order
int temp;
for ( int i = 0; i < arr.length; i++ ){// goes through all array elements
for (int j = 0; j < arr.length - 1; j++ ){ // compare neigbours
if ( arr[ j ] > arr[ j+1] ) { // Any need to swap neighbours?
temp = arr[ j ]; // swap neighbouring elements
arr[ j ] = arr[ j+1];
arr[ j+1] = temp;
}
}
}
}
Exercise
● Let us sort this array using bubble sort
● Note:
• Array contents will keep on changing as we
iterate through the array
• We need to show array contents at the end of
each pass (iteration)
63 29 72 85 18
9
b) Insertion Sort
● Good analogy: sorting of a deck of cards in your hands
● Considers the list to have 2 portions:
• Sorted part
• Unsorted part
● For each iteration through the list:
• Pick an item from the unsorted part
• Place it at the correct position in the sorted part
● We repeatedly move items from the unsorted part to
sorted part until all items are sorted
● Easy to understand
● How does it work?
1. Assume first item is first item in the sorted part, and the
rest are in the unsorted part
2. Loop through all items in the unsorted part
• Take each item and insert it at the right place in the
sorted part
● Still inefficient
• But better than bubble sort
• Time complexity: O(n2) 11
Sample code
public void insertionSort(int arr[]) {// assume: ascending order
int temp, i, j;
for (int i = 1; i < arr.length; ++i) {// start from at 1
j=i;
temp = arr[i]; // current item to sort
while (j>0 && temp < arr[j-1] ) {
arr[j] = arr[j-1] ) // swap adjacent items
j --;
}
arr[j] = temp;
}
} 12
Exercise
● Let us sort this array using insertion sort
13
● When to use it?
• When the list has few items
• Few overheads
• Uses less memory
• When the list is nearly sorted
• Fewer swaps needed
14
c) Selection Sort
● Also simple sorting algorithm
17
Exercise
● Let us sort this array using selection sort
● Rem: Array contents will keep on changing as we
iterate through the array
63 29 72 85 18
18
d) Merge Sort
● Differs with the previous 3
● Uses the divide and conquer principle
• Break a complex problem into smaller & manageable parts
• Tackle the smaller parts individually
• Then bring them together
● How does it work?
1. Keep dividing the array / collection into smaller parts
• Until each has 1 or 2 element
2. Then create bigger arrays by sorting the smaller parts,
going up
3. Until we have the final sorted array!
Exercise
● Let us sort this array using merge sort
63 29 72 85 18 49 3 54
20
● More efficient algorithm
• Keeps on breaking the problem into halves
• Time complexity: O(n log n)
● Note
• Can be implemented iteratively
• As well as recursively!
21
Summary
• In this lesson, we were able to do these sorting algorithms:
• Bubble sort
• Insertion sort
• Selection sort
• Merge sort
22
Next lesson
• OOP
23
Q &A
16