Академический Документы
Профессиональный Документы
Культура Документы
Definition
Sorting is a basic operation in
computer science. sorting refers to
the operation of arranging data in
some given sequence i.e. increasing
order or decreasing order
Basic terminologies in
sorting
Internal and external sorting:
Internal sorting:
An internal sort is any data sorting process that takes place
entirely within the main memory of a computer. This is possible
whenever the data to be sorted is small enough to all be held in
the main memory.
External sorting:
External sorting is required when the data being sorted does
not fit into the main memory of a computing device (usually
RAM) and a slower kind of memory (usually a hard drive)
needs
to be used.
Example:
10
36
8
35
1
CIS 068
Bart
Homer
Lisa
Marge
Maggie
Unsorted:
CIS 068
10
36
1
35
8
Bart
Homer
Maggie
Marge
Lisa
Sorting
what
for
?
Sorted: Binary Search
1
8
10
35
36
CIS 068
Maggie
Lisa
Bart
Marge
Homer
CIS 068
sorts
internal
external
*Natural
*Balanced
*Poly phase
Insertio
n
*Insertion
*Shell sort
selectio
n
*Heap
Excha
nge
*Selection *Bubble
*Quick
Complexity measure
The complexity of a sorting algorithm
measures the running time as a function
of the number n of items to be sorted.
If a1,a2,..an set of data to be
sorted and b is an auxiliary lacation
1) No. of comparison, test whether ai < aj
or ai<b
2) No. of interchange, switch the contents
of ai and aj or of ai and b.
3) Assignments which done b=ai , aj=b or
a =a
Algorithm Measures
Best Case
Often when data already sorted
Worst Case
Data completely disorganised, or in reverse
order.
Average Case
Random order
Quadratic Algorithms
Bubble Sort
CIS 068
Bubble sort
To pass through the array n-1 times,
where n is the number of data in the
array
For each pass:
compare each element in the array with its
successor
bubble
(int x[], int n)
{
interchange the two
elements
if they
are
for (i=0; i<n-1; i++)
not in order
for (j=0; j<n-1; j++)
The algorithm
CIS 068
Array after
Completion
of Each Pass
CIS 068
n 1 O(n)
0 O(1)
Quadratic Algorithms
Selection Sort
CIS 068
CIS 068
last = n-1
Algorithm:
For i=0 .. last -1
Next i
CIS 068
Number of comparisons:
(n-1) + (n-2) + ... + 3 + 2 + 1 =
n * (n-1)/2 =
(n - n )/2
O(n)
Insertion sort
Insertion Sort(A[MAXSIZE],N)
Let a e an array of n elements , a temp variable to
interchange two values,k be the total no. of passes and j
be another control variable
1. Set k=1.
2. For k= 1 to (n-1)
set temp=a[k]
set j= k-1
while temp<a[j] and (j>=0) perform the following steps.
set a[j+1]=a[j]
[End of loop structure]
Assign the value of temp to a[j+1].
[End of for loop structure]
3. exit
example
15
30
99
Pass
a[1]<
15 1:25
30 a[0]
9
20
26
interchange
99
20
26
25
30
99
20
26
15
25
30
99
20
26
Cont.
15
25
30
99
20
26
15
20
25
30
99
26
Pass 6: a[6]<a[4],a[5] so
9
15
20
25
26
30
99
Running Time
The number of primitive operations or
steps executed during a program
execution is the running time of algorithm.
Comparison of Quadratic
Sorts
Comparisons
Exchanges
Best
Worst
Best
Worst
Selectio O(n)
nSort
O(n)
O(1)
O(n)
Bubble
Sort
O(n)
O(n)
O(1)
O(n)
Insertio
n
Sort
O(n)
O(n)
O(1)
O(n)
CIS 068
advantage
disadvantage
Selection
Sort
If array is in
total disorder
If array is
presorted
Bubble Sort
If array is
presorted
If array is in total
disorder
Insertion
Sort
If array is
presorted
If array is in total
disorder
n Log(n) Algorithms
Quick Sort
Quick sort
OR Quicksort Algorithm
Given an array of n elements (e.g., integers):
If array only contains one element, return
Else
pick one element to use as pivot.
Partition elements into two sub-arrays:
Elements less than or equal to pivot
Elements greater than pivot
20
10
80
60
50
30
100
Partitioning Array
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
pivot_index = 0 40
[0]
20
10
[1] [2]
too_big_index
80
60
50
30
100
1.
pivot_index = 0 40
[0]
20
10
[1] [2]
too_big_index
80
60
50
30
100
1.
pivot_index = 0 40
20
[0]
10
[1] [2]
80
60
50
too_big_index
30
100
1.
2.
pivot_index = 0 40
[0]
20
10
[1] [2]
80
60
50
too_big_index
30
100
1.
2.
pivot_index = 0 40
[0]
20
10
[1] [2]
80
60
50
too_big_index
30
100
1.
2.
3.
pivot_index = 0 40
[0]
20
10
[1] [2]
80
60
50
too_big_index
30
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
60
50
too_big_index
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
60
50
too_big_index
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
60
50
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
60
50
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
60
50
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
60
50
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
50
60
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
50
60
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
50
60
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
50
60
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
50
60
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
50
60
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
50
60
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
50
60
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
50
60
80
100
1.
2.
3.
4.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
50
60
80
100
1.
2.
3.
4.
5.
pivot_index = 0 40
[0]
20
10
[1] [2]
30
50
60
80
100
1.
2.
3.
4.
5.
pivot_index = 4
7
[0]
20
10
[1] [2]
30
40
50
60
80
100
Partition Result
20
[0]
10
[1] [2]
30
40
50
<= data[pivot]
60
80
100
> data[pivot]
20
[0]
10
[1] [2]
30
40
50
<= data[pivot]
60
80
100
> data[pivot]
Quicksort Analysis
Assume that keys are random,
uniformly distributed.
What is best case running time?
An example trace of
25 57 48 37 12 92 86 33quicksort
Subsequent processes:
25 57 48 37 12 92 86 33
12 25 (48 37 57 92 86 33)
25 57 48 37 12 92 86 33
25 57 48 37 12 92 86 33
25 57 48 37 12 92 86 33
25 57 48 37 12 92 86 33
25 12 48 37 57 92 86 33
25 12 48 37 57 92 86 33
25 12 48 37 57 92 86 33
25 12 48 37 57 92 86 33
25 12 48 37 57 92 86 33
(12) 25 (48 37 57 92 86 33)
12
12
12
12
12
12
12
12
25 (48 37 33 92 86 57)
25 (48 37 33 92 86 57)
25 (33 37) 48 (92 86 57)
25 (33 37) 48 (92 86 57)
25()33 (37) 48 (57 86) 92()
25 33 37 48 (57 86) 92
25 33 37 48()57(86) 92
25 33 37 48 57 86 92
_ down, _ up
CIS No
068 extra memory needed
Shellsort
We can look at the list as a set of interleaved
sublists
For example, the elements in the even
locations could be one list and the elements in
the odd locations the other list
Shellsort begins by sorting many small lists,
and increases their size and decreases their
number as it continues
78
Shellsort
One technique is to use decreasing powers
of 2, so that if the list has 64 elements, the
first pass would use 32 lists of 2 elements,
the second pass would use 16 lists of 4
elements, and so on
These lists would be sorted with an
insertion sort
79
Shellsort Example
8 sublists
2 elements /
sublist
Increment =
8
4 sublists
4 elements /
sublist
Increment =
4
2 sublists
8 elements /
sublist
Increment =
2
1 sublist
16 elements /
sublist
80
Shellsort Algorithm
passes = lg N
while (passes 1) do
increment = 2passes - 1
for start = 1 to increment do
InsertionSort(list, N, start, increment)
end for
passes = passes - 1
end while
N=15
Pass 1: increment = 7, 7 calls, size =
2
Pass 2: increment = 3, 3 calls, size =
5
Pass 3: increment = 1, 1 call, size =
15
81
Use
Radix sort only applies to integers,
fixed size strings, floating points and
to "less than", "greater than" or
"lexicographic order" comparison
predicates, whereas comparison
sorts can accommodate different
orders.
Books referred
Data structure through c(a practical
approach)-G.S.Baluja
Data structur