Professional Documents
Culture Documents
Sorting
Definition
Rearranging data in an ordered fashion .
Organizing data in sorted manner helps in
searching. For example, finding address from
a telephone directory becomes easier if data
is arranged in a sorted manner
Bubble Sort
METHOD
Scan the list, exchanging adjacent elements if
they are not in relative order; this bubbles the
highest value to the top
scan the list again, bubbling up the second
highest value
repeat until all elements have been placed in
their proper order
Round 1
45
30
77
20
30
45
77
20
30
45
77
20
Round 2
30
45
20
77
30
45
20
77
30
45
20
77
Round 3
30
20
45
77
30
20
45
77
20
30
45
77
Selection Sort
METHOD
Assume the first element in the array be the
smallest(or largest based on sort direction) value
in the array.
Loop through the rest of the elements and if
there is a element smaller than the smallest
value assign that element as the smallest value.
At the end of the loop exchange the positions of
the smallest value with the first element.
Now we have the smallest element right at the
top.
Repeat the same process continuing from the
second element until you reach the last element.
8
45
30
77
20
20
30
77
45
45
30
77
20
20
30
77
45
45
30
77
20
20
30
77
45
20
30
77
45
20
30
77
45
20
30
77
45
20
30
45
77
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
}
Insertion Sort
This sorting method must be used when we want to insert an
element into a sorted array.
METHOD:
Consider a sub-array of length 1 containing the first
element. Since there is only one element it is already
sorted.
Next insert the second item into the sorted array created
above, shifting the first element if needed
Similarly insert the third item into the sorted array, shifting
the other elements if necessary.
Continue this process until all values have been inserted
in the place.
11
sub-list
45
30
77
20
30
45
77
20
30
45
77
20
45
30
77
20
45
30
30
77 Insert 30
20 into sub-list
77
Insert 77
into sub-list
Insert 20
into sub-list
30
45
77
20
20
30
45
77
30
45
77
20
Slide 45 one
down. Insert
30
12
Quick Sort
Very efficient sorting algorithm
Strategy used here is divide & conquer
SIMPLIFIED VIEW:
From the array select an element and let us call it
the pivot .
Arrange elements such that all elements less than
the pivot are to its left forming sub-array 1 and all
greater are to its right forming sub-array 2.
Continue the same process for the sub-array 1 and
sub-array 2.
15
METHOD
1. Let a be an array of size n which needs to be
sorted.
2. min=0, max=n-1
3. If min>=max stop.
4. Select a first element as pivot element.
5. Move the pivot to the rightmost position.
6. Starting from the left, find an element pivot.
Call the position i.
7. Starting from the right, find an element
pivot. Call the position j.
17
Cont...
8. Exchange a[i] and a[j]
9. Continue 6 to 8 as long as i>=j.
10. When i>=j, restore the pivot => swap a[i] and
a[max]
11. Split the list into two: (min,i-1) and (i+1, max)
and sort them separately - apply steps 3 to 11.
18
min:0 max:4
i=-1
Move i till you reach
i>=j or a[i]>pivot
j=4
i=2
j=4
1
i=2
j=3
i=2
1
j=3
9
i=j=3
5
19
min:0
max:2
move
pivot 3
to end
i=-1
j=2
i=3
j=2
i=3
min:0
max:0
j=2
i=j=2
2
3
j=1
i=2
2
20
min:0
max:1
move pivot
1
j=1
i=1
j=1
i=-1
Move i till i>=j
or a[i]>pivot
i=j=1
1
pivot = a[min];
swap(a, min, max);
for(k=0;k<len;k++)
mid = split(a, min, max - 1, pivot);
Merge Sort
24
24
43
43
23
15
23
33
12
15
33
15
33
33
12
splitting
24
43
23
24
43
23
15
24
43
23
15
12
33
12
12
merging
23
append left
over elements
24
43
12
12
15
15
23
24
33
43
No more elements : list exhausted
33
25
Merge the
split arrays
26
find the smallest elements from both the arrays and populate the
temp array. Continue until one of the arrays is exhausted.
27
Copy the sorted array temp into the original array array
28
Comparing performance of
sorting algorithms
29
slowest
fastest
Empirical Analysis
30
slowest
fastest
Empirical Analysis
31