Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Sorting

Download as pdf or txt
Download as pdf or txt
You are on page 1of 91

Sorting

Sorting is a process that organizes a collection of data


into either ascending or descending order.
Sorting Algorithms
• There are many sorting algorithms, such as:
• Insertion Sort
• Selection Sort
• Bubble Sort
• Quick Sort
• Merge Sort

CENG 213 Data Structures


Insertion Sort
• Idea: like sorting a hand of playing cards
• Start with an empty left hand and the cards facing down on the table.
• Remove one card at a time from the table, and insert it into the correct
position in the left hand.
• compare it with each of the cards already in the hand, from right
to left.
• The cards held in the left hand are sorted.
• these cards were originally the top cards of the pile on the table

23 May, 2024 3
Insertion Sort
Remove one card at a time from the table, and
insert it into the correct position in the left hand.

23 May, 2024 4
Insertion Sort
Pass 1: 77 by itself is trivially sorted

33 44 11

23 May, 2024 5
Insertion Sort
Pass 2: 33 is inserted either before or
after 77 so that: 33, 77 is sorted.

44 11

23 May, 2024 6
Insertion Sort
Pass 3: 44 is inserted into its proper place in
33, 77, that is , before 33, between 33 and
77, or after 77, so that: 33, 44, 77 is sorted.

11

23 May, 2024 7
Insertion Sort
Pass 4: 11 is inserted into its
proper place in 33,44,77. so that:
11, 33, 44, 77 is sorted.

77

23 May, 2024 8
Insertion Sort
Pass 5: 88 is inserted into its
proper place in 11,33,44,77. so
that: 11, 33, 44, 77, 88 is sorted.

77 88

23 May, 2024 9
Insertion Sort
Pass 6: 22 is inserted into its proper
place in 11,33,44,77,88. so that: 11,
22, 33, 44, 77, 88 is sorted.

44 77

23 May, 2024 10
Insertion Sort
Pass 7: 66is inserted into its proper
place in 11, 22, 33,44,77,88. so that:
11, 22, 33, 44, 66, 77, 88 is sorted.

44 66

23 May, 2024 11
Insertion Sort
Pass 8: 55 is inserted into its proper place in 11,
22, 33,44,66,77,88. so that: 11, 22, 33, 44, 55, 66,
77, 88 is sorted.

44 55

23 May, 2024 12
Insertion Sort
Suppose an array A with n elements
A[1], A[2],……,A [N] is in memory. The insertion
sort algorithm scans A from A[1] to A[N], inserting
each element A[K] into its proper position in the
previously sorted subarray A[1], A[2],…..A[K-1].
That is:
Pass 1: A[1] by itself is trivially sorted.
Pass 2: A[2] is inserted either before or after A[1] so that: A[1], A[2] is sorted.
Pass 3: A[3] is inserted into its proper place in A[1], A[2], that is , before A[1],
between A[1] and A[2], or after A[2], so that: A[1], A[2], A[3] is sorted.
Pass 4: A[4] is inserted into its proper place in A[1], A[2],A[3] so that:
…………….

A[1], A[2], A[3], A[4] is sorted.


……………………………………………………………………………………
………………………………………………………………………………
Pass N: A[N] is inserted into its proper place in A[1], A[2],……,A[N-1] so that:
………………….

A[1], A[2],……., A[N] is sorted.


Pass A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8]
K=1, -α 77 33 44 11 88 22 66 55

K=2 -α 77 33 44 11 88 22 66 55
K=3 -α 33 77 44 11 88 22 66 55

K=4 -α 33 44 77 11 88 22 66 55

K=5 -α 11 33 44 77 88 22 66 55

K=6 -α 11 33 44 77 88 22 66 55

K=7 -α 11 22 33 44 77 88 66 55
K=8 -α 11 22 33 44 66 77 88 55
Sorted -α 11 22 33 44 55 66 77 88
Algorithm 9.1
(Insertion sort) INSERTION (A,N)
1. Set A[0]:=-α
2. Repeat Steps 3 to 5 for K=2,3,…,N:
3. Set TEMP:=A[K] and PTR:=K-1.
4. Repeat while TEMP<A[PTR]:
(a) Set A[PTR+1]:=A[PTR]: [Move element forward.]
(b). Set PTR:=PTR-1.
[End of loop]
5. Set A[PTR+1]:=TEMP. [Insert element in proper place]
[End of step 2 loop]
6. Return.
Selection Sort
• The selection sort is a combination of searching
and sorting.
• In selection sort, sorting is done after selecting a
particular smallest or largest element from an array
and shifted it to a particular location in an array.
• During each pass, the unsorted element with the
smallest (or largest) value is moved to its proper
position in the array.
Selection Sort
Suppose an array A with n elements A[1], A[2],……, A[N] is
in memory. The selection sort algorithm for sorting A works as
follows.
First find the smallest element in the list and put it in
the first position.
Then find the smallest element in the list and put it in
the second position. And so on. More precisely:
Selection Sort

min = out
1
3
Selection Sort Example
35 65 30 60 20 scan 0-4, smallest 20
swap 35 and 20
20 65 30 60 35 scan 1-4, smallest 30
swap 65 and 30
20 30 65 60 35 scan 2-4, smallest 35
swap 65 and 35
20 30 35 60 65 scan 3-4, smallest 60
swap 60 and 60
20 30 35 60 65 done
Array 40 50 60 10 30 20

1-pass 10 50 60 40 30 20

2-pass 10 20 60 40 30 50

3-pass 10 20 30 40 60 50

4-pass 10 20 30 40 60 50

5-pass 10 20 30 40 50 60

• The number of times the sort passes through the


array is one less than the number of items in the
array.
• In the selection sort, the inner loop finds the
next smallest (or largest) value and the outer
loop places that value into its proper location.
Pass1: Find the location LOC of the smallest in the list of N elements
A[1], A[2],….,A[N], and then interchange A[LOC] and A[1]. Then: A[1] is sorted.
Pass 2: Find the location LOC of the smallest in the list of N-1 elements
A[2],A[3],….,A[N], and then interchange A[LOC] and A[2]. Then:
………………… A[1],A[2] is sorted, Since A[1] ≤ A[2].
Pass 3: Find the location LOC of the smallest in the list of N-2 elements
A[3],A[4],….,A[N], and then interchange A[LOC] and A[3]. Then:
…………………A[1],A[2],A[3] is sorted, Since A[2] ≤ A[3].
………………………………………………………………………………………
…………………..………………………………………………………
Pass N-1: Find the location LOC of the smaller of the element A[N-1], A[N], and
then interchange A[LOC] and A[N-1]. Then:
…………………A[1],A[2]…..A[N] is sorted. Since A[N-1] ≤ A[N].

Thus A is sorted after N-1 passes.


Pass A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8]

K=1, LOC=4 77 33 44 11 88 22 66 55

K=2, LOC=6 11 33 44 77 88 22 66 55

K=3, LOC=6 11 22 44 77 88 33 66 55

K=4, LOC=6 11 22 33 77 88 44 66 55

K=5, LOC=8 11 22 33 44 88 77 66 55

K=6, LOC=7 11 22 33 44 55 77 66 88

K=7, LOC=7 11 22 33 44 55 66 77 88

Sorted 11 22 33 44 55 66 77 88
Procedure 9.2

MIN (A,K,N,LOC)
An array A is in memory. This procedure finds the
location LOC of the smallest element among A[K],
A[K+1],…..A[N].

1. Set MIN:=A[K] and LOC:=K. [Initializes pointers]


2. Repeat for J=K+1, K+2,……N:
if MIN>A[J], then: Set MIN:=A[J] and LOC:=A[J] and LOC=J.
[End of Loop.]
3. Return
Algorithm 9.3
(selection sort) SELECTION (A,N)
1.Repeat Steps 2 and 3 for K=1,2,……,N-1:
2. Call MIN (A,K,N,LOC).
3. [interchange A[K] and A[LOC].]
Set TEMP:=A[K], A[K]:=A[LOC] and
A[LOC]:=TEMP.
[End of Step 1 loop]
4. Exit.
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted
Quicksort
A[p…q] ≤ A[q+1…r]
• Sort an array A[p…r]
• Divide
• Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that each element of
A[p..q] is smaller than or equal to each element in A[q+1..r]
• Need to find index q to partition the array

28
Quicksort
A[p…q] ≤ A[q+1…r]

• Conquer
• Recursively sort A[p..q] and A[q+1..r] using Quicksort
• Combine
• Trivial: the arrays are sorted in place
• No additional work is required to combine them
• The entire array is now sorted

29
Apply Quick Sort Algorithm

32 99 29 98 67 45 33 22 47
Example-1

Consider the Pivot 44

44 33 11 55 77 90 99 22 88 66
Find Smaller Value from Right Side so that Item < Pivot

44 33 11 55 77 90 99 22 88 66
Find Smaller Value from Right Side so that Item < Pivot

44 33 11 55 77 90 99 22 88 66
Find Smaller Value from Right Side so that Item < Pivot

44 33 11 55 77 90 99 22 88 66
Interchange 44 and 22 to obtain the list

22 33 11 55 77 90 99 44 88 66
Find Greater Value from Left Side so that Item> Pivot

22 33 11 55 77 90 99 44 88 66
Find Greater Value from Left Side so that Item> Pivot

22 33 11 55 77 90 99 44 88 66
Find Greater Value from Left Side so that Item> Pivot

22 33 11 55 77 90 99 44 88 66
Interchange 44 and 55 to obtain the list

22 33 11 44 77 90 99 55 88 66
Find Smaller Value from Right Side so that Item < Pivot

22 33 11 44 77 90 99 55 88 66
Find Smaller Value from Right Side so that Item < Pivot

22 33 11 44 77 90 99 55 88 66
Find Smaller Value from Right Side so that Item < Pivot

22 33 11 44 77 90 99 55 88 66
Interchange 44 and 40 to obtain the list

22 33 11 40 77 90 99 55 88 66
Find Greater Value from Left Side so that Item> Pivot

22 33 11 40 77 90 99 55 88 66
Interchange 44 and 77 to obtain the list

22 33 11 40 44 90 99 55 88 66
44

22 33 11 40 90 99 55 88 66

1st Sublist 2nd


Sublist

Now 44 is correctly placed in its final position


1st Sublist

22 33 11 40
Example-2

32 99 29 98 67 45 33 22 47
32 99 29 98 67 45 33 22 47
45

32 99 29 98 67 33 22 47
Find Greater Value from Left Side so that Item>
Pivot

32 45

99 29 98 67 33 22 47
Find Greater Value from Left Side so that Item>
Pivot

45

32 99 29 98 67 33 22 47
Find Greater Value from Left Side so that Item>
Pivot

99 45

32 29 98 67 33 22 47
Find Smaller Value from Right Side so that Item
< Pivot

99 45

32 29 98 67 33 22 47
Find Smaller Value from Right Side so that Item
< Pivot

99 45 47

32 29 98 67 33 22
Find Smaller Value from Right Side so that Item
< Pivot

99 45

32 29 98 67 33 22 47
Find Smaller Value from Right Side so that Item
< Pivot

99 45 22

32 29 98 67 33 47
Exchange
Occurred

45
22 99
32 29 98 67 33 47
Exchange
Occurred
And
Index Changed

45

32 22 29 98 67 33 99 47
Find Greater Value from Left Side so that Item>
Pivot

29 45

32 22 98 67 33 99 47
Find Greater Value from Left Side so that Item>
Pivot

45

32 22 29 98 67 33 99 47
Find Greater Value from Left Side so that Item>
Pivot

98 45

32 22 29 67 33 99 47
Find Smaller Value from Right Side so that Item
< Pivot

98 45 33

32 22 29 67 99 47
Exchange
Occurred

98 45 33

32 22 29 67 99 47
Exchange
Occurred

45
33 98
32 22 29 67 99 47
Exchange
Occurred

45

32 22 29 33 67 98 99 47
Exchange
Occurred
And
Index Changed

45

32 22 29 33 67 98 99 47
Find Greater Value from Left Side so that Item>
Pivot

45

32 22 29 33 67 98 99 47
Find Greater Value from Left Side so that Item>
Pivot

67 45

32 22 29 33 98 99 47
Find Smaller Value from Right Side so that Item
< Pivot

67 45

32 22 29 33 98 99 47
Find Smaller Value from Right Side so that Item
< Pivot

67 45

32 22 29 33 98 99 47
Find Smaller Value from Right Side so that Item
< Pivot

67 45

32 22 29 33 98 99 47
Exchange
Occurred

45 67

32 22 29 33 98 99 47
Exchange
Occurred

45
67
32 22 29 33 98 99 47
Left Side< 45 < Right
Side

45 67 98 99 47

32 22 29 33
Left Side< 45 < Right
Side

67 98 99 47

45

32 22 29 33
Apply Recursive Partitioning on
Left Side

32 22 29 33
Apply Recursive Partitioning on Right
Side
67 98 99 47
Merge sort
• Merge sort algorithm is one of two important divide-and-
conquer sorting algorithms (the other one is quicksort).
• It is a recursive algorithm.
• Divides the list into two equal parts,
• Sort each part separately, and
• Then merge the sorted halves into one sorted array.

CENG 213 Data Structures


Merge Sort Algorithm

• To sort an array of n elements, we perform the


following steps in sequence:
• If n < 2 then the array is already sorted.
• Otherwise, n > 1, and we perform the
following three steps in sequence:
1. Sort the left half of the the array using MergeSort.
2. Sort the right half of the the array using MergeSort.
3. Merge the sorted left and right halves.

80
Merge Sort

Original 24 13 26 1 12 27 38 15
Divide in 2 24 13 26 1 12 27 38 15
Divide in 4 24 13 26 1 12 27 38 15
Divide in 8 24 13 26 1 12 27 38 15
Merge 2 13 24 1 26 12 27 15 38
Merge 4 1 13 24 26 12 15 27 38
Merge 8 1 12 13 15 24 26 27 38

81
Execution Example

• Partition
7 2 9 43 8 6 1

82
Execution Example (cont.)

• Recursive call, partition


7 2 9 43 8 6 1

7 29 4

83
Execution Example (cont.)

• Recursive call, partition


7 2 9 43 8 6 1

7 29 4

72

84
Execution Example (cont.)

• Recursive call, base case


7 2 9 43 8 6 1

7 29 4

72

7→7

85
Execution Example (cont.)

• Recursive call, base case


7 2 9 43 8 6 1

7 29 4

72

7→7 2→2

86
Execution Example (cont.)

• Merge
7 2 9 43 8 6 1

7 29 4

72→2 7

7→7 2→2

87
Execution Example (cont.)

• Recursive call, …, base case, merge


7 2 9 43 8 6 1

7 29 4

72→2 7 9 4 → 4 9

7→7 2→2 9→9 4→4

88
Execution Example (cont.)

• Merge
7 2 9 43 8 6 1

7 29 4→ 2 4 7 9

72→2 7 9 4 → 4 9

7→7 2→2 9→9 4→4

89
Execution Example (cont.)

• Recursive call, …, merge, merge


7 2 9 43 8 6 1

7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

90
Execution Example (cont.)

• Merge
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9

7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

91

You might also like