m180: data structures & algorithms in java sorting algorithms arab open university 1

Post on 17-Jan-2018

258 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

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

1

M180: Data Structures & Algorithms in Java

Sorting Algorithms

Arab Open University

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)

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

Big-Oh to the Selected Sorts

● Selection Sort = n²

● Insertion Sort = n²

● Merge Sort = n log(n)

● Quick Sort = n log(n)

5

Selection Sort

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.

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

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; }}

9

Insertion Sort

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.

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…

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

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---

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---

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---

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--;

}}

}

17

Quick Sort

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.

Quicksort: Example

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

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

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

Quicksort: Example

5. If i > j then end the phase

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

Quicksort: Example

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

Quicksort: Example

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

Quicksort: Example

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

Quicksort: Example

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

Quicksort: Example

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

Quicksort: Example

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

Quicksort: Example

– 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

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

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);}

32

Merge Sort

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?

34

Merge Sort

35

Merge Sort: Example

Say unsorted array values are:

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

36

Merge Sort: The Code

See the attached code

top related