You are on page 1of 24

Data Structures and

Algorithms

Sorting:
Rearranging elements of list so that they are
in ascending or descending order
Sorting Algorithms

■ Exchange Sort
■ Bubble Sort
■ Quick Sort
■ Selection and Tree Sort
■ Straight Selection Sort
■ Binary Tree Sort
■ Heap Sort
■ Insertion Sort
■ Simple Insertion Sort
■ Shell Sort
■ Merge and Radix Sort
■ Merge Sort
■ Radix Sort

Sorting
Bubble Sort

■ Easy to understand, but


■ Least efficient
■ Its pass through the element sequentially
■ Each pass consist of comparing each element
with its successor (xi with xi+1) and interchange
them if they are not in proper order

Sorting
Exchanging Code

Object exchange(x,i,j) {
//interchange x[i] and x[j];
temp=x[j];
x[j]=x[i];
x[i]=temp;
return x;
}

Sorting
Bubble Sort (algorithm)

bubble(x,n) {
switch=TRUE;
for (pass=0; pass<n-1 && switch==TRUE; pass++) {
switch=FALSE;
for (j=0; j<pass-1; j++) {
if (x[j]>x[j+1]) {
switch=TRUE;
exchange x[j] and x[j+1];
}
}
}

Sorting
Bubble Sort (Example)

example: 25 57 48 37 12 92 86 33

1st step: 25 57 48 37 12 92 86 33 x0 < x 1 🡪 no interchange

2nd step: 25 57 48 37 12 92 86 33 x1 > x 2 🡪 interchange

3rd step: 25 48 57 37 12 92 86 33 x2 > x3 🡪 interchange

4th step: 25 48 37 57
12 57
12 92 86 33 x3 > x4 🡪 interchange

5th step: 25 48 37 12 57 92 86 33 x4 < x5 🡪 no interchange

6th step: 25 48 37 12 57 86
92 86
92 33 x5 > x6 🡪 interchange

7th step: 25 48 37 12 57 86 92
33 92
33 x6 > x7 🡪 interchange
Sorting
Bubble Sort (Example)

Original data: 25 57 48 37 12 92 86 33

After 1st pass: 25 48 37 12 57 86 33 92

After 2nd pass: 25 37 12 48 57 33 86 92

After 3rd pass: 25 12 37 48 33 57 86 92

After 4th pass: 12 25 37 33 48 57 86 92

After 5th pass: 12 25 33 37 48 57 86 92

After 6th pass: 12 25 33 37 48 57 86 92

After 7th pass: 12 25 33 37 48 57 86 92


Sorting
Quick Sort

■ Its named also as partition exchange sort


■ Quite efficient
■ a is an element from specific position j
■ Each elements in position 0 through j-1 is less then or
equal to a
■ Each elements in position j+1 through n-1 is greater
then or equal to a
■ Procedure:
1. Increase pointer down by one position until x[down] > a
2. Decrease pointer up by one position until x[up] <= a
3. If up>down, interchange x[down] with x[up]
4. Process is repeated until condition in step 3 fail

Sorting
Quick Sort (Example)

quick(x,lb,ub) { partition(x,lb,ub,pj) {
if (lb >= ub) return; a=x[lb]; up=ub; down=lb;
partition(x,lb,ub,j); while (down < up) {
quick(x,lb,j-1); while (x[down] <= a && down < ub) down++;
quick(x,j+1,ub); while (x[up] > a) up--;
} if (down < up) exchange x[down] with x[up]
}
x[lb] = x[up]; x[up] = a; *pj = up;
}
a 25 57 48 37 12 92 86 33 lb=0, ub=7, a=x[lb]=25;

down up

25 12 48 37 57 92 86 33 x[lb] = x[up]=12; x[up] = a = 25; *pj = 1;

quick(x,0,0); 🡪 not processed


down up quick(x,2,7);

12 25 48 37 57 92 86 Sorting
33
Quick Sort (Example)

a lb=2, ub=7, a=x[lb]=48;


48 37 57 92 86 33

down up partition(x,lb,ub,pj) {
a=x[lb]; up=ub; down=lb;
48 37 33 92 86 57 while (down < up) {
while (x[down] <= a && down < ub) down++;
down up while (x[up] > a) up--;
if (down < up) interchange x[down] with x[up]
33 37 }
48 92 86 57
x[lb] = x[up]; x[up] = a; *pj = up;
}
x[lb] = x[up]=33; x[up] = a = 48; *pj = 4;

quick(x,2,3);
quick(x,5,7);

Sorting
Quick Sort (Example)

a partition(x,lb,ub,pj) {
33 37 lb=2, ub=3, a=x[lb]=33;
a=x[lb]; up=ub; down=lb;
x[lb] = x[up]=33; while (down < up) {
down up x[up] = a = 33; while (x[down] <= a && down < ub) down++;
*pj = 2; while (x[up] > a) up--;
quick(x,2,1); not if (down < up) interchange x[down] with x[up]
quick(x,3,3); 🡪 processed }
x[lb] = x[up]; x[up] = a; *pj = up;
}

a quick(x,lb,ub) {
92 86 57 lb=5, ub=7, a=x[lb]=92; if (lb >= ub) return;
partition(x,lb,ub,j);
down up x[lb] = x[up]=57; quick(x,lb,j-1);
x[up] = a = 92; quick(x,j+1,ub);
*pj = 7; }
57 86 92 quick(x,5,6);
quick(x,8,7);🡪not processed

Sorting
Quick Sort (Example)

partition(x,lb,ub,pj) {
a x[lb] = x[up]=57;
57 86 a=x[lb]; up=ub; down=lb;
x[up] = a = 57; while (down < up) {
down up *pj = 5; while (x[down] <= a && down < ub) down++;
quick(x,5,4); not while (x[up] > a) up--;
🡪
quick(x,6,6); processed if (down < up) interchange x[down] with x[up]
}
57 86 x[lb] = x[up]; x[up] = a; *pj = up;
}

Final Step: quick(x,lb,ub) {


if (lb >= ub) return;
12 25 33 37 48 57 86 92 partition(x,lb,ub,j);
0 1 2 3 4 5 6 7 quick(x,lb,j-1);
quick(x,j+1,ub);
}

Sorting
Selection Sort

■ Successive elements are selected in order and


placed into their proper position
■ Straight selection sort :
■ Its named also as push-down sort
■ The largest of remaining elements, large, is
placed into its proper position
■ Binary Tree sort :
■ Scanning each element and placing it into its
proper position in binary tree

Sorting
Straight Selection Sort
(algorithm)
n=6;
selection (x,n) { large=48; index=0; 48 37 57 92 86 33
for (i=n-1; i>0; i--) { large=57; index=2;
large=x[0]; index=0; large=92; index=3;
for (j=1; j<i; j++) { j i
if (x[j]>large) {
large=x[j]; index=j; } large=48; index=0;
large=57; index=2; 48 37 57 33 86 92
}
if (x[j]>large) { large=86; index=4;
x[index]=x[i]; x[i]=large;} j i
} large=48; index=0;
} 48 37 57 33 86 92
large=57; index=2;
j i
large=48; index=0;
48 37 33 57 86 92

j i
large=33; index=0;
33 37 48 57 86 92
Sorting

j i
Insertion Sort

■ Simple insertion sort:


■ It sorts a set of records by inserting records
into an existing sorted file
■ Shell sort:
■ It sorts separate sub records of original records
■ Address calculation sort:
■ It is also called by hashing
■ Sort a set of records by inserting records into
an existing sorted file

Sorting
Simple Insertion Sort (example)

insertSort(x,n) {
for (i=1; i< n; i++) {
y=x[i];
// move up 1 position all elements greater than y
for (j=i-1; j>=0 && y<x[j]; j--)
x[j+1]=x[j];
// insert y at proper position
x[j+1]=y; 25 57 48 37 12 92 86 33 Do nothing
}
j i i=1; Y=57; j=0
}

25 57
48 48 37 12 92 86 33

j i i=2; Y=48; j=1

25 48
37 57 37 12 92 86 33

j i i=3; Y=37; j=2


Sorting
Simple Insertion Sort (example)

12
25 37 48 57 12 92 86 33
i=4; Y=12; j=3
j i insertSort(x,n) {
i=5; Y=92; j=4 for (i=1; i< n; i++) {
y=x[i];
12 25 37 48 57 92 86 33 // move up 1 position all elements
Do nothing greater than y
j i for (j=i-1; j>=0 && y<x[j]; j--)
i=6; Y=86; j=5 x[j+1]=x[j];
12 25 37 48 57 92
86 86 33 // insert y at proper position
x[j+1]=y;
j i }
}
i=7; Y=33; j=6
12 25 33
37 48 57 86 92 33

j i

Sorting
Shell Sort

■ More significant improvement on simple


insertion sort than binary or list insertion
■ It sorts separate subrecords of original records
■ These subrecords contain every kth element of
original records
■ The k subrecords are divided so that the ith
element of the jth subrecords is
x[(i-1)*k+j-1]

Sorting
Shell Sort (example)

shellSort(x,n) {
for (incr=0; incr< numinc; incr++) {
// span is the size of the incrmnts
span=incrmnts[incr];
for (j=span; j< n; j++){
y=x[j];
for(k=j-span; k>=0 && y<x[k]; k-=span)
x[k+span]=x[k];
x[k+span]=y; Original records: 25 57 48 37 12 92 86 33
incrmnts=(5,3,1)
}
} Pass 1, span=5: 25 57 48 37 12 92 86 33
} j=5; Y=92; k=0
k j
Pass 1, span=5: 25 57 48 37 12 92 86 33
j=6; Y=86; k=1
k j
Pass 1, span=5: 25 57 33
48 37 12 92 86 48
33
j=7; Y=33; k=2 Sorting
k j
Shell Sort (example)

Pass 2, span=3: 25 12
57 33 37 12 92
57 86 48
j=3; Y=37; k=0
j=4; Y=12; k=1
k j
Pass 2, span=3: 25 12 33 37 57 92
48 86 48
57
j=5; Y=92; k=2
j=6; Y=86; k=3
j=6; Y=86; k=0 k j
j=7; Y=48; k=4
j=7; Y=48; k=1

Pass 3, span=1: 25
12 25
12 33 37 48 92 86 57
j=1; Y=12; k=0
k j
Pass 3, span=1: 12 25 33 37 48 92
86 86
92 57
j=2; Y=33; k=1
j=3; Y=37; k=2
j=6; Y=86; k=5
k j
Sorting
Shell Sort (example)

Pass 3, span=1: 12 25 33 37 48 57
86 92
86 92
57
j=7; Y=57; k=6
j=7; Y=57; k=5
j=7; Y=57; k=4
k j

Sorting
Merge and Radix Sort

■ Merge sort:
■ combining two or more sorted element
■ Radix sort:
■ based on the value of actual digits in
positional representations of number being
sorted

Sorting
Merge Sort

Original: 25 57 48 37 12 92 86 33

Pass 1: 25 57 37 48 12 92 33 86

Pass 2: 25 37 48 57 12 33 86 92

Pass 3: 12 25 33 37 48 57 86 92

Sorting
Assignment (30 September 2020)

Make your own code for:


■ Bubble sort
■ Quick Sort
■ Selection Sort
■ Insertion Sort
■ Shell Sort
■ Merge Sort

Sorting

You might also like