0% found this document useful (0 votes)
37 views101 pages

Prepered By: Guided by S.Saravanan (Asst - Professor) Dept. of CSE

The document discusses different sorting algorithms. It first defines sorting as segregating items into groups according to a specified criterion. It then describes three categories of sorting algorithms: brute force algorithms like selection sort and bubble sort, divide and conquer algorithms like merge sort and quick sort, and radix sort. The document provides examples and diagrams to illustrate how bubble sort works by repeatedly comparing adjacent elements and exchanging them if they are in the wrong order.

Uploaded by

Snehal Yeole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views101 pages

Prepered By: Guided by S.Saravanan (Asst - Professor) Dept. of CSE

The document discusses different sorting algorithms. It first defines sorting as segregating items into groups according to a specified criterion. It then describes three categories of sorting algorithms: brute force algorithms like selection sort and bubble sort, divide and conquer algorithms like merge sort and quick sort, and radix sort. The document provides examples and diagrams to illustrate how bubble sort works by repeatedly comparing adjacent elements and exchanging them if they are in the wrong order.

Uploaded by

Snehal Yeole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 101

PREPERED BY

R.VENGATESH(13BCS2095)
S.SURYA(13BCS2087)
S.VIGNESH(13BCS2096) GUIDED BY
V.VIGNESH
BALAKRISHNAN(13BCS2097)
M.VADIVEL(13BCS2089)
S.SARAVANAN
(Asst.professor)
Dept. of CSE
 Definition of Sorting
 Brute force
◦ Selection sort
◦ Bubble sort
◦ Radix sort
 Divide and Conquer
◦ Merge sort
◦ Quick sort
 Conclusion
17/07/2014 2
Sorting: 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 }
1. We have two group of items:
◦ sorted group, and
◦ unsorted group
2. Initially, all items are in the unsorted group.
The sorted group is empty.
◦ We assume that items in the unsorted group
unsorted.
◦ We have to keep items in the sorted group sorted.

17/07/2014 4
1. Select the “best” (eg. smallest) item from the
unsorted group, then put the “best” item at the
end of the sorted group.
2. Repeat the process until the unsorted group
becomes empty.

17/07/2014 5
5 1 3 4 6 2

Comparison

Data Movement

Sorted
17/07/2014 6
5 1 3 4 6 2

Comparison

Data Movement

Sorted
17/07/2014 7
5 1 3 4 6 2

Comparison

Data Movement

Sorted
17/07/2014 8
5 1 3 4 6 2

Comparison

Data Movement

Sorted
17/07/2014 9
5 1 3 4 6 2

Comparison

Data Movement

Sorted
17/07/2014 10
5 1 3 4 6 2

Comparison

Data Movement

Sorted

17/07/2014 11
5 1 3 4 6 2

Comparison

Data Movement

Sorted
17/07/2014 12
5 1 3 4 6 2

Largest

Comparison

Data Movement

Sorted
17/07/2014 13
5 1 3 4 2 6

Comparison

Data Movement

Sorted
17/07/2014 14
5 1 3 4 2 6

Comparison

Data Movement

Sorted
17/07/2014 15
5 1 3 4 2 6

Comparison

Data Movement

Sorted
17/07/2014 16
5 1 3 4 2 6

Comparison

Data Movement

Sorted
17/07/2014 17
5 1 3 4 2 6

Comparison

Data Movement

Sorted
17/07/2014 18
5 1 3 4 2 6

Comparison

Data Movement

Sorted
17/07/2014 19
5 1 3 4 2 6

Comparison

Data Movement

Sorted
17/07/2014 20
5 1 3 4 2 6

Largest

Comparison

Data Movement

Sorted
17/07/2014 21
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 22
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 23
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 24
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 25
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 26
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 27
2 1 3 4 5 6

Largest

Comparison

Data Movement

Sorted
17/07/2014 28
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 29
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 30
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 31
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 32
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 33
2 1 3 4 5 6

Largest

Comparison

Data Movement

Sorted
17/07/2014 34
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 35
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 36
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 37
2 1 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 38
2 1 3 4 5 6

Largest

Comparison

Data Movement

Sorted
17/07/2014 39
1 2 3 4 5 6

Comparison

Data Movement

Sorted
17/07/2014 40
1 2 3 4 5 6
DONE!
Comparison

Data Movement

Sorted
17/07/2014 41
 Running time:
◦ Worst case: O(N2)
◦ Best case: O(N2)

17/07/2014 42
Adjacent elements are compared and exchanged.
It is repeatedly done
At the end the largest element is bubbled to the last

position in the list

17/07/2014 43
9, 6, 2, 12, 11, 9, 3, 7
6,
Bubblesort
exchanging 9,
Bubblesortcompares
when 2,
comparesthe
necessary. 12,
thenumbers
numbersin
Here the 11, 9, 3, 7
inpairs
pairsfrom
first
fromleft
leftto
number
toright
is
exchanging when necessary. Here the first number is compared
to
right
compared
tothe
thesecond
secondand
andas
asititisislarger
largerthey
theyare
areexchanged.
exchanged.
Now
6,
Nowthe
larger
thenext
and so2,
nextpair
pairof
this 9,
ofnumbers
pair is also 12,
numbersarearecompared.11, 9, 3, 7
compared. Again
exchanged.
larger and so this pair is also exchanged.
Againthe
the99isisthe
the

In
6,
Inthe
thethird
exchange 2,
is made. 9,
thirdcomparison,
comparison,the
We 12,
the99isisnot
move on to 11, 9, 3, 7
notlarger
largerthan
compare
thanthe
the
the12
next
12so
sono
pair
exchange is made. We move on to compare the next pair without
any
no
without
anychange
changeto tothe
thelist.
list.
The
6,
The12 2,
12isislarger
largerthan9,
thanthe
the11
11so11,
sothey
theyare 12, 9, 3, 7
areexchanged.
exchanged.

The
Theend
The

endof
6,
Thetwelve
ofthe
thelist
2,
listhas
9,
twelveisisgreater
greaterthan
hasbeen
beenreached
11,
thanthe
the99so
reachedso
sothey
sothis
9, 12, 3, 7
theyare areexchanged
thisisisthe
exchanged
theend
endof ofthe
thefirst
firstpass.
pass. The
The

6, 2, 9, 11, 9, 3, 12, 7
twelve
twelveatatthe
theend
endof ofthe
thelist
listmust
mustbe belargest
largestnumber
numberin inthe
thelist
listand
andsosoisisnow
nowinin
the The
correct 12 is greater
position.
The 12 We
is greater than
now the 3
start so
a they
new are
pass exchanged.
from left to right.
the correct position. Wethan
now the 3 so
start theypass
a new are exchanged.
from left to right.

The
6,
The12 2,
12isisgreater 9,
greaterthan
thanthe
the77so11,
sothey
theyare 9, 3, 7, 12
areexchanged.
exchanged. 17/07/2014 44
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
6, 6,
2, 2, 9, 9,
11,3,
11,
9,7,
11,
3,11,
7, 12
Notice
Noticethat
thatthis
thistime
timewe
wedodonot
nothave
havetotocompare
comparethe
thelast
lasttwo
two
numbers
numbers as we know the 12 is in position. This pass thereforeonly
as we know the 12 is in position. This pass therefore only
requires 6 comparisons.
requires 6 comparisons.

17/07/2014 45
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
2, 6, 9, 3,
9, 9,
7, 9,
3, 7, 11, 12
This
Thistime
timethe
the11
11and
and12
12are
arein
inposition.
position. This
Thispass
passtherefore
thereforeonly
only
requires
requires55comparisons.
comparisons.

17/07/2014 46
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
2, 6, 3,
9, 9,
7, 9,
3, 7, 9, 11, 12
Each
Eachpass
passrequires
requiresfewer
fewercomparisons.
comparisons. This
Thistime
timeonly
only44are
areneeded.
needed.

17/07/2014 47
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
Fifth Pass
2, 6, 3, 7, 9, 9, 11, 12
2, 6,
3, 3,
6, 7, 9, 9, 11, 12
The
Thelist
listisisnow
nowsorted
sortedbut
butthe
thealgorithm
algorithmdoes
doesnot
notknow
knowthis
thisuntil
untilitit
completes
completesaapasspasswith
withno
noexchanges.
exchanges.

17/07/2014 48
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
Fifth Pass
2,
This
Thispass
6,
passno
3,
noexchanges
7,
exchangesarearemade
9,
madeso
sothe
9,
thealgorithm
11,
algorithmknows
knowsthe
12
thelist
listisis
sorted.
sorted. ItItcan
cantherefore
thereforesave
savetime
timeby
bynot
notdoing
doingthe
thefinal
finalpass.
pass. With
2, 3, 6, 7, 9, 9, 11, 12
With
other
otherlists
liststhis
thischeck
checkcould
couldsave
savemuch
muchmore
morework.
work.
Sixth Pass

2, 3, 6, 7, 9, 9, 11, 12
17/07/2014 49
 Running time:
◦ Worst case: O(N2)
◦ Best case: O(N)
 Variant:
◦ bi-directional bubble sort
 original bubble sort: only works to one direction
 bi-directional bubble sort: works back and forth.

17/07/2014 50
 This sort is unusual because it does not directly
compare any of the elements

 We instead create a set of buckets and repeatedly


separate the elements into the buckets

 On each pass, we look at a different part of the


elements

17/07/2014 5
 Assuming decimal elements and 10 buckets, we
would put the elements into the bucket associated
with its units digit

 The buckets are actually queues so the elements are


added at the end of the bucket

 At the end of the pass, the buckets are combined in


increasing order

17/07/2014 5
 On the second pass, we separate the elements based
on the “tens” digit, and on the third pass we separate
them based on the “hundreds” digit

 Each pass must make sure to process the elements


in order and to put the buckets back together in the
correct order

17/07/2014 5
 Each element is examined once for each of the
digits it contains, so if the elements have at most M
digits and there are N elements this algorithm has
order O(M*N)

 This means that sorting is linear based on the


number of elements

 Why then isn’t this the only sorting algorithm used?

17/07/2014 5
The unit digit is 0

The unit digit is 1

 The unit digit is 2

 The unit digit is 3

17/07/2014 5
The unit digits are already in order

Now start sorting the tens digit

17/07/2014 5
The unit and tens digits are already in order

Now start sorting the hundreds digit

Values in the buckets are now in order

17/07/2014 5
The most well known algorithm design strategy:
1. Divide instance of problem into two or more
smaller instances

2. Solve smaller instances recursively

3. Obtain solution to original (larger) instance


by combining these solutions

17/07/2014 58
a problem of
size n

subproblem 1 subproblem 2
of size n/2 of size n/2

a solution to a solution to
subproblem 1 subproblem 2

a solution to
the original problem

17/07/2014 59
 Sorting: mergesort and quicksort

 Tree traversals

 Binary search

 Matrix multiplication-Strassen’s algorithm

 Convex hull-QuickHull algorithm

17/07/2014 60
Merge sort 8
8 3
3 2
2 9
9 7
7 1
1 5
5 4
4

8
8 3
3 2
2 9
9 7
7 1
1 5
5 4
4

 It stores by dividing array into two


halves 8
8 3
3 2
2 9
9 7
711 5
5 4
4

 Sort each them and merge them


into single sorted one 8
8 3
3 2
2 9
9 7
7 1
1 5
5 4
4

3
3 8
8 2
2 9
9 1
1 7
7 4
4 5
5

2
2 3
3 8
8 9
9 1
1 4
4 5
5 7
7

1
1 2
2 3
3 4
4 5
5 7
7 8
8 9
9
17/07/2014 61
Merge-Sort Example

17/07/2014 62
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge
Merge

17/07/2014 63
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 Merge Merge

Merge

17/07/2014 64
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge

17/07/2014 65
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14
14 23
23 45
45 98 6 33
33 42 67

6 14 23 33 42 45 67 98

Merge

17/07/2014 66
98 23 45 14 6 67 33 42

6 14 23 33 42 45 67 98

17/07/2014 67
 Quicksort is more widely used than any other sort.
 Quicksort is well-studied, not difficult to implement,

works well on a variety of data, and consumes fewer


resources that other sorts in nearly all situations.
 Quicksort is O(n*log n) time, and O(log n) additional

space due to recursion.

17/07/2014 68
Given a pivot, partition the elements of the array
such that the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot

The sub-arrays are stored in the original data array.

Partitioning loops through, swapping elements


below/above pivot.

17/07/2014 69
We are given array of n integers to sort:

40 20 10 80 60 50 7 30 100

17/07/2014 70
There are a number of ways to pick the pivot
element. In this example, we will use the
first element in the array:
40 20 10 80 60 50 7 30 100

17/07/2014 71
Given a pivot, partition the elements of the
array such that the resulting array consists
of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements <
pivot

The sub-arrays are stored in the original data


array.

Partitioning loops through, swapping elements


below/above pivot.

17/07/2014 72
pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 73
1. While data[too_big_index] <= data[pivot]
++too_big_index

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 74
1. While data[too_big_index] <= data[pivot]
++too_big_index

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 75
1. While data[too_big_index] <= data[pivot]
++too_big_index

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 76
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 77
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 78
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 79
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 80
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 81
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 82
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 83
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 84
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 85
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 86
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 87
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 88
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 89
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 90
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 91
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 92
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 93
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 94
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 95
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 96
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]

pivot_index = 4 7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

17/07/2014 97
7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

17/07/2014 98
7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

17/07/2014 99
 Quicksort illustrates the operation of the basic
algorithm. When the array is partitioned, one element is
in place on the diagonal, the left subarray has its upper
corner at that element, and the right subarray has its
lower corner at that element. The original file is
divided into two smaller parts that are sorted
independently. The left subarray is always sorted first,
so the sorted result emerges as a line of black dots
moving right and up the diagonal.

17/07/2014 10
0
 In this ,we can understand that every sorting
will depends upon one another.
 So, we should understand sorting is the best

starting for every representation.


 From this, Quick sort will easily perform

sorting.

17/07/2014 10
1

You might also like