joseph lindo sorting algorithms sir joseph lindo university of the cordilleras

Post on 23-Dec-2015

220 Views

Category:

Documents

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Joseph Lindo

SortingAlgorithms

Sir Joseph LindoUniversity of the Cordilleras

Joseph Lindo

Activity

Algorithms

Definition

Reason

SortingSorting

An operation that segregates items into groups according to specified criterion

A = { 3 1 6 2 1 3 4 5 9 0 }

A = { 0 1 1 2 3 3 4 5 6 9 }

Definition

Joseph Lindo

Activity

Algorithms

Definition

Reason

Sorting

Why sorting?

Examples:

Sorting Books in Library (Dewey system)

Sorting Individuals by Height (Feet and Inches)

Reason

Joseph Lindo

Activity

Algorithms

Definition

Reason

Sorting

Why sorting?

Examples:

Sorting Movies in Blockbuster (Alphabetical)

Sorting Numbers (Sequential)

Reason

Joseph Lindo

Activity

Algorithms

Definition

Reason

Sorting

Why sorting?It is used in a wide range of

applications

Several sorting algorithms have been invented because the task is so fundamental and also frequently used

Reason

Joseph Lindo

Activity

Algorithms

Definition

Reason

SortingSorting

Algorithms

Algorithms

Joseph Lindo

Activity

Algorithms

Definition

Reason

SortingSorting

AlgorithmsBubble SortSelection SortInsertion SortMerge SortQuick Sort

Algorithms

Joseph Lindo

Activity

Algorithms

Definition

Reason

SortingGroup

Case StudyGroup maximum of five

membersShort bond paperCreate the flowchart of:

Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort

Activity

Joseph Lindo

Activity

Algorithms

Definition

Reason

SortingGroup

Case Study Group maximum of five

members Yellow paper Construct your own “Sorting Algorithm” Provide example Provide the CodeActivity

Joseph Lindo

SortingAlgorithms

--end--

Sir Joseph LindoUniversity of the Cordilleras

Joseph Lindo

Insertion

Bubble

Merge

Selection

Quick

Sorting Algorithms

Bubble SortThe simple idea is keep

passing through the list, swapping the next element that is out of order, until the list is sorted.

Bubble

Joseph Lindo

Insertion

Bubble

Merge

Selection

Quick

Sorting Algorithms

Selection SortWorks by selecting

elements in specific order, and putting them in proper position in the final list

It is an in-place sorting algorithm

Selection

Joseph Lindo

Insertion

Bubble

Merge

Selection

Quick

Sorting Algorithms

Insertion SortIt is a comparison based

sort, which sorts the array one entry at a time.Insertion

Joseph Lindo

Insertion

Bubble

Merge

Selection

Quick

Sorting Algorithms

Merge SortOriginal problem is split into

subproblems

Solutions to subproblems lead to the solution of the main problemMerge

Joseph Lindo

Insertion

Bubble

Merge

Selection

Quick

Sorting Algorithms

Quick Sort

Invented by C.A.R. Hoare

Also based on the divide-and-conquer paradigm

Quick

Joseph Lindo

Characteristics

jo

Stable Algorithm

A stable sort keeps equal elements in the same order

This may matter when you are sorting data according to some characteristic

Example: sorting students by test scores

Joseph Lindo

Characteristics

jo

Stable Algorithm

Bob

Ann

Joe

Zöe

Dan

Pat

Sam

90

98

98

86

75

86

90

original array

Bob

Ann

Joe

Zöe

Dan

Pat

Sam

90

98

98

86

75

86

90

stably sorted

Joseph Lindo

Characteristics

Unstable Algorithm

An unstable sort may or may not keep equal elements in the same order

Stability is usually not important, but sometimes it is important

Joseph Lindo

Characteristics

Unstable Algorithm

Bob

Ann

Joe

Zöe

Dan

Pat

Sam

90

98

98

86

75

86

90

original array

Bob

Ann

Joe

Zöe

Dan

Pat

Sam

90

98

98

86

75

86

90

unstably sorted

Joseph Lindo

Characteristics

Bubble Sort

Compare each element (except the last one) with its neighbor to the right

Compare each element (except the last two) with its neighbor to the right

Compare each element (except the last three) with its neighbor to the right

Repeat the process

Joseph Lindo

Diagram

Bubble Sort

7 2 8 5 4

2 7 8 5 4

2 7 8 5 4

2 7 5 8 4

2 7 5 4 8

2 7 5 4 8

2 5 7 4 8

2 5 4 7 8

2 7 5 4 8

2 5 4 7 8

2 4 5 7 8

2 5 4 7 8

2 4 5 7 8

2 4 5 7 8

(done)

Joseph Lindo

Code

Bubble Sort

public static void bubbleSort(int[] a) { int outer, inner; for (outer = a.length - 1; outer > 0; outer--){ for (inner = 0; inner < outer; inner++){ if (a[inner] > a[inner + 1]) { int temp = a[inner]; a[inner] = a[inner + 1]; a[inner + 1] = temp; } } }}

Joseph Lindo

Characteristics

Selection Sort

Given an array of length n,Search elements 0 through n-1 and select the smallest Swap it with the element in location 0)

Search elements 1 through n-1 and select the smallest Swap it with the element in location 1

Search elements 2 through n-1 and select the smallest Swap it with the element in location 2

Continue in this fashion until there’s nothing left to search

Joseph Lindo

Diagram

Selection Sort

7 2 8 5 4

2 7 8 5 4

2 4 8 5 7

2 4 5 8 7

2 4 5 7 8

Joseph Lindo

Code

Selection Sort

public static void selectionSort(int[] a){ int outer, inner, min; for(outer=0;outer<a.length-1;outer++){ min = outer; for(inner=outer+1;inner<a.length;inner++){

if (a[inner] < a[min]){ min = inner; } } int temp = a[outer]; a[outer] = a[min]; a[min] = temp; }}

Joseph Lindo

Characteristics

Insertion Sort

Repeated the following steps until no elements are left in the unsorted part of the array

– First available element is selected from the unsorted section of the array

– Place selected element in its proper position in the sorted section of the array

Joseph Lindo

Diagram

Insertion Sort

27

3 4 7 1214142021333810559232816

sorted next to be inserted

3 4 7 559232816

10temp

3833212014141210

sorted

less than 10

Joseph Lindo

Code

Insertion Sort

void insertionSort(Object array[], int startIdx, int endIdx){ for (int i = startIdx; i < endIdx; i++){ int k = i; for (int j = i + 1; j < endIdx; j++){ if (array[k]>array[j]){ k = j; } int temp = array[k]; array[k] = array[j]; array[j] = temp;

} } }

Joseph Lindo

Characteristics

Merge Sort

Three steps:– Divide

Divide the sequence of data elements into two halves– Conquer

Conquer each half sorting– Combine

Combine or merge the two halves to come up with the sorted sequence

Joseph Lindo

Diagram

Merge Sort

Merge

SORT

Divide intotwo halves

FirstPart SecondPart

FirstPart SecondPart

A

A is sorted!

Joseph Lindo

Diagram

Merge Sort

6 10 14 223 5 15 28

L: R:

Temporary Arrays

5 15 28 30 6 10 1452 3 7 8 1 4 5 6A:

Joseph Lindo

Diagram

Merge Sort

3 5 15 28 30 6 10 14

L:

A:

3 15 28 30 6 10 14 22

R:

i=0 j=0

k=0

2 3 7 8 1 4 5 6

1

Joseph Lindo

Diagram

Merge Sort

1 5 15 28 30 6 10 14

L:

A:

3 5 15 28 6 10 14 22

R:

k=1

2 3 7 8 1 4 5 6

2

i=0 j=1

Joseph Lindo

Diagram

Merge Sort

1 2 15 28 30 6 10 14

L:

A:

6 10 14 22

R:

i=1

k=2

2 3 7 8 1 4 5 6

3

j=1

Joseph Lindo

Diagram

Merge Sort

1 2 3 6 10 14

L:

A:

6 10 14 22

R:

i=2 j=1

k=3

2 3 7 8 1 4 5 6

4

Joseph Lindo

Diagram

Merge Sort

1 2 3 4 6 10 14

L:

A:

6 10 14 22

R:

j=2

k=4

2 3 7 8 1 4 5 6

i=2

5

Joseph Lindo

Diagram

Merge Sort

1 2 3 4 5 6 10 14

L:

A:

6 10 14 22

R:

i=2 j=3

k=5

2 3 7 8 1 4 5 6

6

Joseph Lindo

Diagram

Merge Sort

1 2 3 4 5 6 14

L:

A:

6 10 14 22

R:

k=6

2 3 7 8 1 4 5 6

7

i=2

Joseph Lindo

Diagram

Merge Sort

-1 2 3 4 5 6 7 14

L:

A:

3 5 15 28 6 10 14 22

R:2 3 7 8 1 4 5 6

8

i=3

k=7

Joseph Lindo

Diagram

Merge Sort

1 2 3 4 5 6 7 8

L:

A:

3 5 15 28 6 10 14 22

R:2 3 7 8 1 4 5 6

i=4

k=8

Joseph Lindo

Code

Merge Sort

void mergeSort(Object array[], int startIdx,int endIdx){ if (array.length != 1) { mergeSort(leftArr,startIdx, midIdx); mergeSort(rightArr, midIdx+1,endIdx); combine(leftArr, rightArr); }}

Joseph Lindo

Characteristics

Quick Sort

Divide-and-Conquer Paradigm– Divide

Partition array into two subarrays A[p...q-1] and A[q+1...r] such that each element in A[p...q-1] is less than or equal to A[q] and each element in A[q+1...r] is greater than or equal to A[q]

A[q] is called the pivot– Conquer

Sort the subarrays

Joseph Lindo

Diagram

Quick Sort

x < p p p ≤ x

PartitionFirstPart SecondPart

p

pivot

A:

x < p p p ≤ x

SortedFirstPart

SortedSecondPart

Sorted

Joseph Lindo

Diagram

Quick Sort

p

p x < p p ≤ x

p p ≤ xx < p

A:

A:

A: p

Joseph Lindo

Diagram

Quick Sort

A: 4 8 6 3 5 1 7 2

Joseph Lindo

Diagram

Quick Sort

4 8 6 3 5 1 7 2

i=0

j=1

A:

Joseph Lindo

Diagram

Quick Sort

A:

j=1

4 8 6 3 5 1 7 2

i=0

8

Joseph Lindo

Diagram

Quick Sort

A: 4 8 6 3 5 1 7 26

i=0

j=2

Joseph Lindo

Diagram

Quick Sort

4 8 6 3 5 1 7 2

i=0

383

j=3

i=1

A:

Joseph Lindo

Diagram

Quick Sort

A: 4 3 6 8 5 1 7 2

i=1

5

j=4

Joseph Lindo

Diagram

Quick Sort

A: 4 3 6 8 5 1 7 2

i=1

1

j=5

Joseph Lindo

Diagram

Quick Sort

A: 4 3 6 8 5 1 7 2

i=2

1 6

j=5

Joseph Lindo

Diagram

Quick Sort

A: 4 3 8 5 7 2

i=2

1 6 7

j=6

Joseph Lindo

Diagram

Quick Sort

A: 4 3 8 5 7 2

i=2

1 6 22 8

i=3

Joseph Lindo

Diagram

Quick Sort

A: 4 3 2 6 7 8

i=3

1 5

Joseph Lindo

Diagram

Quick Sort

A: 4 1 6 7 8

i=3

2 542 3

Joseph Lindo

Diagram

Quick Sort

A: 3 6 7 81 542

x < 4 4 ≤ x

pivot incorrect position

Joseph Lindo

Code

Quick Sort

void quickSort(Object array[], int leftIdx,int rightIdx){

int pivotIdx; /* Termination condition! */ if (rightIdx > leftIdx) { pivotIdx = partition(array,

leftIdx, ightIdx); quickSort(array, leftIdx,

pivotIdx-1); quickSort(array, pivotIdx+1,

rightIdx); } }

top related