0% found this document useful (0 votes)
0 views24 pages

Lecture 08 - Sorting Arrays.ppt

This lecture covers various sorting algorithms including Bubble Sort, Insertion Sort, Selection Sort, and Merge Sort, detailing their implementation and efficiency. Each algorithm is explained with examples and time complexities, highlighting their strengths and weaknesses. The lecture aims to equip students with the ability to implement and use these sorting algorithms effectively.

Uploaded by

plutoagcorp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views24 pages

Lecture 08 - Sorting Arrays.ppt

This lecture covers various sorting algorithms including Bubble Sort, Insertion Sort, Selection Sort, and Merge Sort, detailing their implementation and efficiency. Each algorithm is explained with examples and time complexities, highlighting their strengths and weaknesses. The lecture aims to equip students with the ability to implement and use these sorting algorithms effectively.

Uploaded by

plutoagcorp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CSI247:

Data Structures

Lecture 08 – Sorting Arrays

Department of Computer Science


B. Gopolang (247-275)
Previous Lesson
● Packages
• Java-defined packages
• User-defined packages
● Creating and using user-defined packages

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

● How does it work?


1. Select the first element in the array
2. Compare it with all elements in the list.
3. Is selected element smaller (or larger) than others?
Yes: swap the two elements
4. Repeat the process with the remaining positions in the
list till the entire list is sorted.
Sample method
public void selectionSort (int arr[ ]) {// assume: ascending order
int minIndex, temp;
for (int i = 0; i < arr.length - 1; i++) {
minIndex = i; // initial index with min value
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex])
minIndex = j; // found an index with new min value
}
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}}
● Still an inefficient algorithm
• Loops through all items
• Several times
• Time complexity: still O(n2)

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

You might also like