Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
TECHNIQUES
By…
Sukanta behera
Reg. No. 07SBSCA048
Topic overview:
NOTES ON SORTING.
ALGORITHM SelectionSort(A[o..n-1])
//The algorithm sorts a given array by selection sort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order
for i=0 to n-2 do
min=i
for j=i+1 to n-1 do
if A[j] < A[min], min=j
swap A[i] and A[min]
Bubble sort:
Bubble sort is comes under Brute Force method. The
sorting problem is to compare adjacent elements of
the list and exchange them if they are out of order .
By doing it repeatedly, we end up “bubbling up” the
largest element to the last position of the list and vice
versa.
ALGORITHM BubbleSort(A[0..n-1])
89 45 68 90 29 34 17 12
89 68 29 17
45 90 34 12
8 4 6 9 2 3 1 1
9 5 8 0 9 4 7 2
45 68
29 34 12 17
89 90
45 68 89 90 12 17 29 34
12 17 29 34 45 68 89
90
Quick Sort:
Quick sort is another important sorting algorithm that is
based on the Divide and Conquer method. It divides the
input elements according to their value. Specifically, it
rearranges elements of a given array A[0..n – 1] to achieve
its partition.
ALGORITHM Quicksort(A[l…r])
//Input: A subarray A[l…r] of A[0..n – 1], defined by its left & right
indices
if l < r
ALGORITHM Partition(A[l…r])
//Partitions a subarray by using its first element as a pivot
//Input: A subarray A[l..r] of A[o…n – 1], defined by its left
and right
// indices l and r (l < r)
//Output: A partition of A[l..r], with the split position returned
as this
// function’s values
p = A[l]
i = l; j = r + 1
repeat
repeat i = i + 1 until A[i] >= p
repeat j = j - 1 until A[j] <= p
Swap(A[i], A[j])
until i >= j
swap(A[i], A[j]) //undo last swap when i >= j
swap(A[l],A[j])
0 1 2 3 4 5 6 7 0 1 2
3 4 5 6 7
5 i3 1 9 8 2 4 j7 1 2 3
4
5 3 1 i9 8 2 j4 7 1
5 3 1 i4 8 2 j9 7 3
ij4
5 3 1 4 i8 j2 9 7 i3
j4
5 3 1 4 j2 i8 9 7
4
Insertion sort:
Insertion sort comes under Decrease and Conquer
method. In this method, we consider an application of
the decrease-by-one technique to sorting an array A[0…
n – 1].