Sorting

1 / 55

# Sorting - PowerPoint PPT Presentation

Lecture 8. Sorting. Prof. Sin-Min Lee Department of Computer Science. Preview for today’s presentation. What is sorting? Why do we sort? Three sorting methods Insertion Sort Selection Sort Bubble Sort Which method is better?. What is sorting?.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Sorting' - maya-stokes

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Lecture 8

### Sorting

Prof. Sin-Min Lee

Department of Computer Science

Preview for today’s presentation
• What is sorting?
• Why do we sort?
• Three sorting methods
• Insertion Sort
• Selection Sort
• Bubble Sort
• Which method is better?
What is sorting?
• It is a process of re-arranging a given set of objects in a specific order. (in ascending or descending order)
• Ex: Objects are sorted in telephone books, in the tables of contents, in dictionaries, in income tax files, in libraries, in warehouses and almost everywhere that stored objects have to be searched and retrieved.
Why do we sort?
• It is a relevant and essential activity in data processing.
• It is efficient to search in a ordered data set than an unordered data set.
Before proceeding . . .

Terminology and Notation

We are given items

a1, a2, a3, . . . . . . , an

then, we want to sort these items into an order

ak1, ak2,ak3, . . . . . ., akn

such that,

f(ak1)  f(ak2)  f(ak3)  . . . . . .  f(akn)

Judging the speed of sorting algorithms
• Best case - it occurs when the data are already in order.
• Average case - it occurs when the data are in random order.
• Worst case - it occurs when the data are in reverse order.
Judging the speed sorting algorithm contin.
• Does the algorithm exhibit natural or unnatural behavior?
• Natural behavior exhibits if the sort works least when the list is already in order, works harder as the list becomes less ordered, and works hardest when a list is in inverse order.
• Unnatural behavior exhibits when minimum number of comparisons is needed when data are in reverse order and maximum if data are already in order.
• Does it rearrange data with equal keys?
Introduction To Sorting
• Only concerned with internal sorting where all records can be sorted in main memory
• We are interested in analyzing sorting algorithms for worst-case performance and average performance
Insertion Sort

Initial order: SQ SA C7 H8 DK

Step 1: SQ SA C7 H8 DK

Step 2: C7 SQ SA H8 DK

Step 3: C7 DK H8 SQ SA

Insertion Sort: Another Example

Original 34 8 64 51 32 21 Positions Moved

After p=2 8 34 64 51 32 21 1

After p=3 8 34 64 51 32 21 0

After p=4 8 34 51 64 32 21 1

After p=5 8 32 34 51 64 21 3

After p=6 8 21 32 34 51 64 4

Insertion Sort

Step 1: sort the first two items of the array.

Step 2: insert the third item into its sorted

position in relation to the first two items.

Step 3: the fourth element is then inserted into the list of three elements

Continue: until all items have been sorted

Pseudocode - Insertion Sort

for (i = 1; i < n; i++)

place data[i] in its proper

position;

move all elements data[j] >

data[i] by one position;

Implementation of the Insertion Sort

Template<class genType>

void Insertion (genType data[ ], int n)

{ register int i,j;

genType temp;

for (i = 1; i < n; i++)

temp = data[i];

j = i;

while (j > 0 && temp < data[j-1])

{ data[j] = data[j-1];

j--;

}

data[j] = temp;

}

Example: sort the array [5 2 3 8 1 ]

a[0] a[1] a[2] a[3] a[4]

i =1 5 2 3 8 1

j =1,0

temp=2

i=2 2 5 3 8 1

j=2

temp=3

i=3 2 3 5 8 1

j=1,3

temp=8

Insertion sort example contin.1

a[0] a[1] a[2] a[3] a[4]

i=4 2 3 5 8 1

j=4

temp=1

i=4 2 3 5 8

j=3

temp=1

i=4 2 3 5 8

j=2

temp=1

Insertion sort example contin.2

a[0] a[1] a[2] a[3] a[4]

i=4 2 3 5 8

j=1

temp=1

i=4 2 3 5 8

j=0

temp=1

i=5 1 2 3 5 8

j=0

temp=1

Number of Exchange for each case

Insertion Sort

Best Case: Cmin = n-1

O(n) Mmin = 2(n-1)

Average Case: Cave = 1/4(n^2+n-2)

O(n^2) Mave = 1/4(n^2+9n-10)

Worst Case: Cmax = 1/2(n^2+n)-1

O(^2) Mmax = 1/2(n^2+3n-4)

// C --> Comparisons M --> Moves //

Insertion Sort Running Time
• The average case and worst case running times are quadratic.
• The best case running time is linear.
• The algorithms is quick when the starting array is nearly sorted
Selection Sort

Step1: Select the element with the lowest

value and exchanges it with the

1st element

Step2: From the remaining n-1 elements,

the element with the next-lowest

value is found and exchanged with

the 2nd element.

Continue: up to the last two elements.

Pseudocode - Selection Sort

For(i =0; i < n-1; i++)

select the smallest element among data[i], . . . , data[n-1];

swap it with data[i];

Implementation of the SelectionSort

Template<class genType>

void Selection(genType data[], int n)

{ register int i, j, least;

for (i = 0; i < n-1; i++)

{ for (j = i+1, least = i; j < n; j++)

if (data[j] < data[least]) least = j;

if (i != least) Swap (data[least], data[i]);

}

}

Example: sort the array[5 2 3 8 1]

a[0] a[1] a[2] a[3] a[4]

i=0 52 3 8 1

j=1,2,3,4,5  2 4 8  swap

least = 0,1 1 2 3 8 5

i=1 1 23 8 5

j=2,3,4,5 1  8 5

least = 1 1 2 3 8 5

Selection Sort example contin.

a[0] a[1] a[2] a[3] a[4]

i=2 1 2 38 5

j=3,4,51 2  8

least=2 1 2 3 8 5

i=3

j=4,5 1 2 3 8 5

least=3,4 1 2 3   Swap

finally i=4 1 2 3 5 8

Selection Sort: Example 2

44 55 12 42 94 18 06 67

|-------------------------------------------------------------------|

06 55 12 42 94 18 44 67

|-------------------------------------|

06 12 55 42 94 18 44 67

|---------------------------------|

06 12 18 42 94 55 44 67

06 12 18 42 94 55 44 67

|----------------------|

06 12 18 42 44 55 94 67

06 12 18 42 44 55 94 67

|-----------|

06 12 18 42 44 55 67 99

Number of exchange for each case

Selection Sort

for all cases: 1/2(n^2 - n) comparisons

Best Case: Mmin = 3(n - 1)

Worst Case: Mmax = n^2/4 + 3(n - 1)

Average Case: n(ln n+y), where y is

Euler’s constant