Title: Sorting
1Sorting
Lecture 8
- Prof. Sin-Min Lee
- Department of Computer Science
2(No Transcript)
3(No Transcript)
4(No Transcript)
5(No Transcript)
6(No Transcript)
7Preview for todays presentation
- What is sorting?
- Why do we sort?
- Three sorting methods
- Insertion Sort
- Selection Sort
- Bubble Sort
- Which method is better?
8What 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.
9Why 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.
10Sorting methods / algorithms
11Before 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)
12Judging 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.
13Judging 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?
14Introduction 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
15Insertion 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
CClub DDiamond SSpade HHeart
16Insertion Sort Another Example
Original 34 8 64 51 32 21 Positions
Moved
After p2 8 34 64 51 32 21 1 After
p3 8 34 64 51 32 21 0 After p4 8
34 51 64 32 21 1 After p5 8 32 34
51 64 21 3 After p6 8 21 32 34 51
64 4
17Insertion 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
18Pseudocode - Insertion Sort
- for (i 1 i lt n i)
- place datai in its proper
- position
- move all elements dataj gt
- datai by one position
19(No Transcript)
20Implementation of the Insertion Sort
- Templateltclass genTypegt
- void Insertion (genType data , int n)
- register int i,j
- genType temp
- for (i 1 i lt n i)
- temp datai
- j i
- while (j gt 0 temp lt dataj-1)
- dataj dataj-1
- j--
-
- dataj temp
21Example sort the array 5 2 3 8 1
- a0 a1 a2 a3 a4
- i 1 5 2 3 8 1
- j 1,0
- temp2
- i2 2 5 3 8 1
- j2
- temp3
- i3 2 3 5 8 1
- j1,3
- temp8
22Insertion sort example contin.1
- a0 a1 a2 a3 a4
- i4 2 3 5 8 1
- j4
- temp1
- i4 2 3 5 8
- j3
- temp1
- i4 2 3 5 8
- j2
- temp1
23Insertion sort example contin.2
- a0 a1 a2 a3 a4
- i4 2 3 5 8
- j1
- temp1
- i4 2 3 5 8
- j0
- temp1
- i5 1 2 3 5 8
- j0
- temp1
24(No Transcript)
25(No Transcript)
26Number of Exchange for each case
- Insertion Sort
- Best Case Cmin n-1
- O(n) Mmin 2(n-1)
- Average Case Cave 1/4(n2n-2)
- O(n2) Mave 1/4(n29n-10)
- Worst Case Cmax 1/2(n2n)-1
- O(2) Mmax 1/2(n23n-4)
- // C --gt Comparisons M --gt Moves //
27Insertion 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
28(No Transcript)
29(No Transcript)
30(No Transcript)
31(No Transcript)
32(No Transcript)
33(No Transcript)
34(No Transcript)
35(No Transcript)
36(No Transcript)
37(No Transcript)
38(No Transcript)
39(No Transcript)
40(No Transcript)
41(No Transcript)
42(No Transcript)
43(No Transcript)
44(No Transcript)
45(No Transcript)
46(No Transcript)
47(No Transcript)
48Selection 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.
49Pseudocode - Selection Sort
- For(i 0 i lt n-1 i)
- select the smallest element among datai, . .
. , datan-1 - swap it with datai
50Implementation of the Selection Sort
- Templateltclass genTypegt
- void Selection(genType data, int n)
- register int i, j, least
- for (i 0 i lt n-1 i)
- for (j i1, least i j lt n j)
- if (dataj lt dataleast) least
j - if (i ! least) Swap
(dataleast, datai) -
51Example sort the array5 2 3 8 1
- a0 a1 a2 a3 a4
- i0 5 2 3 8 1
- j1,2,3,4,5 ? 2 4 8 ? swap
- least 0,1 1 2 3 8 5
- i1 1 2 3 8 5
- j2,3,4,5 1 ? 8 5
- least 1 1 2 3 8 5
52Selection Sort example contin.
- a0 a1 a2 a3 a4
- i2 1 2 3 8 5
- j3,4,5 1 2 ? 8
- least2 1 2 3 8 5
- i3
- j4,5 1 2 3 8 5
- least3,4 1 2 3 ? ? Swap
- finally i4 1 2 3 5 8
-
53Selection 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
54Number of exchange for each case
- Selection Sort
- for all cases 1/2(n2 - n) comparisons
- Best Case Mmin 3(n - 1)
- Worst Case Mmax n2/4 3(n - 1)
- Average Case n(ln ny), where y is
- Eulers constant
- (about 0.577216)
55Selection Sort Running Time
- All cases running time for Selection Sort are
quadratic. - O(n2)