M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

36
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1

description

Types of Sorting Algorithms There are many, many different types of sorting algorithms, but we will study the following primary algorithms: ● Selection Sort ● Insertion Sort ● Quick Sort ● Merge Sort

Transcript of M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Page 1: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

1

M180: Data Structures & Algorithms in Java

Sorting Algorithms

Arab Open University

Page 2: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

2

Sorting

• A fundamental application for computers.

• Done to make finding data (searching) faster.

• Many different algorithms for sorting.

• One of the difficulties with sorting is working with a fixed size storage container (array)– if resize, that is expensive (slow)

Page 3: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Types of Sorting Algorithms

There are many, many different types of sorting algorithms, but we will study the following primary algorithms:

● Selection Sort● Insertion Sort

● Quick Sort● Merge Sort

Page 4: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Big-Oh to the Selected Sorts

● Selection Sort = n²

● Insertion Sort = n²

● Merge Sort = n log(n)

● Quick Sort = n log(n)

Page 5: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

5

Selection Sort

Page 6: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

6

Selection Sort

• How does it work?

– Search through the list and find the smallest element.

– Swap the smallest element with the first element.

– Repeat starting at second element and find the second smallest element.

Page 7: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

7

Selection Sort: Example

35 65 30 60 20 scan 0-4, smallest 20

swap 35 and 2020 65 30 60 35 scan 1-4,

smallest 30swap 65 and 30

20 30 65 60 35 scan 2-4, smallest 35

swap 65 and 3520 30 35 60 65 scan 3-4,

smallest 60swap 60 and 60

20 30 35 60 65 done

Page 8: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

8

Selection Sort: The Code

public static void selectionSort(int[] list){ int min;

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

if( list[j] < list[min] ) min = j; temp = list[i]; list[i] = list[min]; list[min] = temp; }}

Page 9: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

9

Insertion Sort

Page 10: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

10

Insertion Sort• Based on technique of card players to arrange a

hand– Player keeps cards picked up so far in sorted order– When the player picks up a new card

• Makes room for the new card• Then inserts it in its proper place

To make room:Hold the target value

in a variableShuffle elements to the right until gap at right

place.

Page 11: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

11

Insertion Sort

• The first item is sorted• Compare the second item to the first

– if smaller swap• Third item, compare to item next to it

– need to swap– after swap compare again

• And so forth…

Page 12: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Insert Action: i=1

20 8 5 10 7

20 20 5 10 7

8temp

8

i = 1, first iteration

8 20 5 10 7---

Insertion Sort : Example

Page 13: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Insert Action: i=2

8 20 5 10 7

8 20 20 10 7

temp

5

5

i = 2, second iteration

8 8 20 10 75

5 8 20 10 7---

Page 14: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Insert Action: i=3

5 8 20 10 7

5 8 20 20 7

temp10

10

i = 3, third iteration

5 8 10 20 7---

Page 15: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Insert Action: i=4

5 8 10 20 7

5 8 10 20 20

5 8 10 10 20

5 8 8 10 20

7temp

7

7

7

i = 4, forth iteration

5 7 8 10 20---

Page 16: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

16

Insertion Sort: The Code

public void insertionSort(int[] list){ int temp, j;

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

j = i;while( j > 0 && temp < list[j - 1]){ // swap elements

list[j] = list[j - 1];list[j - 1] = temp;j--;

}}

}

Page 17: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

17

Quick Sort

Page 18: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Quicksort

The general idea behind Quicksort is this: • Break an array into two parts

• Move elements around so that all the larger values are in one end and all the smaller values are in the other.

• Each of the two parts is then subdivided in the same manner, and so on until the subparts contain only a single value, at which point the array is sorted.

Page 19: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Quicksort: Example

– To illustrate the process, suppose an unsorted array, called a, looks like the following figure.

Page 20: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Phase 11. If the length of the array is less than 2, then done.2. Locate the value in the middle of the array and call it

the pivot. The pivot is 7 in this example.3. Tag the elements at the left and right ends of the array

as i and j, respectively.

Quicksort: Example

Page 21: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

4. While a[i] < pivot value, increment i.While a[j] >= pivot value, decrement j:

Quicksort: Example

Page 22: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

5. If i > j then end the phase

else interchange a[i] and a[j]:

Quicksort: Example

Page 23: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

6. Increment i and decrement j.If i > j then end the phase:

Quicksort: Example

Page 24: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

7. Repeat step 4, i.e., While a[i] < pivot value, increment i While a[j] >= pivot value, decrement j:

Quicksort: Example

Page 25: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

8. Repeat step 5, i.e., If i > j then end the phase else interchange a[i] and a[j]:

Quicksort: Example

Page 26: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

9. Repeat step 6, i.e., Increment i and decrement j. If i < j then end the phase:

Quicksort: Example

Page 27: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

10. Repeat step 4, i.e., While a[i] < pivot value, increment i While a[j] >= pivot value, decrement j:

Quicksort: Example

Page 28: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

11. Repeat step 5, i.e., If i > j then end the phase else interchange a[i] and a[j].

Quicksort: Example

Page 29: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

– This ends the phase. – Split the array into the two subarrays a[0..j]

and a[i..10]. – For clarity, the left subarray is shaded. – Notice that all the elements in the left subarray

are less than or equal to the pivot, and those in the right are greater than or equal.

Quicksort: Example

Page 30: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Phase 2 and Onward

– Reapply the process to the left and right subarrays and then divide each subarray in two and so on until the subarrays have lengths of at most one.

Quicksort: Example

Page 31: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Quicksort: The Codevoid quickSort (int[] a, int left, int right){  if (left >= right) return;  int i = left; int j = right; int pivotValue = a[(left + right) / 2]; while (i < j){ while (a[i] < pivotValue) i++; while (pivotValue < a[j]) j--; if (i <= j){ int temp = a[i]; a[i] = a[j]; a[j] = temp; i++; j--; } } quickSort (a, left, j); quickSort (a, i, right);}

Page 32: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

32

Merge Sort

Page 33: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

33

Merge Sort Algorithm

1. If a list has 1 element or 0 elements it is sorted

2. If a list has more than 2 split into into 2 separate lists

3. Perform this algorithm on each of those smaller lists

4. Take the 2 sorted lists and merge them together

How Does it Work?

Page 34: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

34

Merge Sort

Page 35: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

35

Merge Sort: Example

Say unsorted array values are:

12, 9, 4, 99, 120, 1, 3, 10

Page 36: M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

36

Merge Sort: The Code

See the attached code