Sorting Techniques
–
Selection Sort
– Bubble Sort
Working :
“SELECT” an Element and Put in PROPER PLACE
Description :
1. From position 0, find the smallest and then exchange it with the element in position 0.
2. From position 1, find the smallest and exchange with position 1.
3. Now from 2 do the same until you have reached the end of the list
General Algorithm: for positions i = 0 to max-1 find smallest from position i exchange with position i
S t o r i o n t c l e
S e
67
21
21
21
21
21
33 21
33
33
33
33
33
84
67 84
67 84
49
49
49
84
50
50
49
49
49
67
67
67
50
50
50
50
84
75
75
75
75
75
75
84
Starting from position 0 find the smallest and exhange with element in position 0 start at position 1 ….
#define MAX 5
{ int Min(int a[], int pos) int i,m,index=pos; m=a[pos]; // Get the Current Value for (i=pos+1;i<MAX;i++) // Search from next
}
{ if (a[i]<m)
{m=a[i];index=i; } return index; void main()
{int A[MAX],i,temp,k; for (i=0;i<MAX;i++) cin>>A[i]; //INPUT
}
} for (i=0;i<MAX;i++)
{ k=Min(A,i); //FIND if (A[k]<A[i]) //SWAP
{ temp=A[k];
A[k]=A[i];
A[i]=temp; cout<<”Sorted elements “
<< endl; for (i=0;i<MAX;i++) cout<<A[i]<<endl;
}
C
I
S
E
E
R
E
X
23 78 45 8 32 56
8 78 45 23 32 56
8 23 45 78 32 56
8 23 32 78 45 56
8 23 32 45 78 56
8 23 32 45 56 78
Original list
After pass 1
After pass 2
After pass 3
After pass 4
After pass 5
Working :
It works by comparing neighbours in the array and exchanging them if necessary to put the smaller of the pair first.
On each pass through the array an element 'bubbles' up into place.
General Algorithm : for (i=0;i<max;i++) for (j=0;j<max-1;j++) if (a[j]>a[j+1] )
{ save = a[j] a[j] = a[j+1] a[j+1] = save
}
t
S o l e r b b
B u
1
2
5
3
8
3
8
5
2
1
I = 0 j= 4 ( compare j with j-1and swap )
3
1
5
2
5
2
1
3
5
1
2
3
8
8 8
Pass-1
Pass-2
1
2
3
5
8
Pass-3
2
3
1
5
8
// The Algorithm Sinks the LARGEST TO THE BOTTOM
#define MAX 5 void main()
{ int A[MAX],i,j,temp,sorted=0; for (i=0;i<MAX;i++) cin>>A[i]; i=0; cout<<”Sorted Elements"<<endl; for (i=0;i<MAX;i++)
} cout<<A[i]<<endl; while ((i<MAX)&&(sorted==0))
{sorted=1; for (j=0;j<MAX-i-1;j++)
{ if (A[j]>A[j+1]) // Largest sinks
{ temp=A[j];
A[j]=A[j+1];
A[j+1]=temp; sorted=0;
}
} i++;
}
23 78 45
EXERCISE
8 32 56
8 23 78 45 32 56
8 23 32 78 45 56
8 23 32 45 78 56
8 23 32 45 56 78
Original list
After pass 1
After pass 2
After pass 3
After pass 4
Sorted!
Merge Sort
• What is the concept used in Merge and Quick Sort?
This two sorting techniques use
“
DIVIDE and CONQUER
“ Technique.
• What is Divide and Conquer Technique?
The Problem is divide into similar subproblems
When to stop Dividing?
When the problem is small enough to be handled.
Outline for divide and conquer Sorting ( NEXT )
Sortlist( )
{ if the list has length greater than 1 then
{ partition the list into lowlist,highlist; sort(lowlist); sort(highlist); combine (lowlist,highlist);
} }
Where does the Quick and merge sort differ?
They differ in the Way the the List is partitioned
Working Break the list into two sublists of size as nearly equal as possible and then sort them separately. Then Merge the two sorted list. Hence know as MERGE sort.
EG : 8 7 6 5 4 3 2 1 ( MID = L+H/2 = 1+8 /2 = 4 )
8 7 6 5 ( 1+4/2=2) 4 3 2 1 ( 1+4/2=2)
8 7 6 5
Now Sort & Merge:
7 8
5 6 7 8
5 6
4 3 2 1
3 4
1 2 3 4
1 2
1 2 3 4 5 6 7 8
S
I
E
R
S
E
E
X
C
26
26
26
26
26
33
29
33
33
33
29
33
35
35
35
35
35
29
29
29
19
12
19
19
12
12
12
12
19
19
22
22
22
22
22
12 19 22 26 29 33 35
Void merge(int lpos, int rpos, int rend)
{ int I,lend,numelements,tmppos,tmparray[MAX] lend = rpos-1; tmppos = lpos; numelements = rend - lpos+1; while ((lpos <= lend) && ( rpos <=rend)) if ( a[lpos] <= a[rpos) tmparray[tmppos++] = a[lpos++]; else tmparray[tmppos++] = a[rpos++]; while (lpos <= lend) tmparray[tmppos++] = a[lpos++]; while (rpos <= rend) tmparray[tmppos++] = a[rpos++]; for (I=0;I<numelements;I++,rend--) a[rend]= tmparray[rend];
} 7 8 5 6
Void mergesort( int left, int right ) int center; if ( left < right)
{ center = (left=right/2); mergesort(left,center); mergesort(center+1,right); merge(left,center+1,right);
}
Void main( )
{//input
MergeSort(0,Max-1)
//output }
5 6 7 8