Unit-1 3
Unit-1 3
“ The first largest element is placed in proper position i.e. at A[N-1] after the first pass” . In
general “ Kth largest element is placed in its proper position i.e. at A[n-k] after pass 'k' ”.
The complete set of passes in bubble sort method is,
Algorithm: Bubble_Sort(A,N)
// A is an array of N elements
{
for i:= 1 to N-1 do
{
for j:=0 to N-i-1 do
if( A[j] > A[j+1] ) then
swap( A[j],A[j+1]);
}
}
/* Program for BUBBLE SORT */ void Bubble_Sort(int a[],int n)
#include<stdio.h> {
#include<stdlib.h> int i,j,temp;
#include<conio.h> for(i=1;i<n;i++)
void Bubble_Sort( int[], int) {
void main() for(j=0;j<n-i;j++)
{ {
int a[10],n,i; if(a[j]>a[j+1])
clrscr(); {
printf("\n Enter Array size :"); temp=a[j];
scanf("%d",&n); a[j]=a[j+1];
printf("\n Enter elements \n"); a[j+1]=temp;
for (i=0;i<n;i++) }
scanf("%d",&a[i]); }
bubblesort(a,n); }
printf("\n Array elements after sorting: }
\n");
for (i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
Selection Sort
Selection Sort is the easiest method to sort list of elements.
In selection sort method first find the smallest(largest) element in the list and swap it with
the first element. Then find the second smallest(largest) element in the list and swap it with
the second element of the list. The process of searching and swapping the next smallest is
repeated until all the elements in the list have been sorted in ascending (descending)
order.
i.e the Selection Sort searches all of the elements in a list until it finds the smallest
element. It swaps this with the first element in the list.
Next it finds the smallest of the remaining elements and swaps it with the second element
and so on.
The procedure to sort 'n' elements in ascending order is,
Pass 1: Find the position (minpos) of the smallest element in the list of 'n' elements. Then
interchange A[minpos] & A[0]. i.e. A[0] is sorted means it is in proper place.
Pass 2: Find position (minpos) of the smallest element in the sublist of (n-1) elements then
interchange A[minpos] & A[1]. Now A[0] & A[1] are sorted i.e. A[0] ≤ A[1].
..
..
pass (n-1) : Find the position (minpos) of the smallest element in A[n-2] & A[n-1] then
interchange A[minpos] & A[n-2]. Now A[0], A[1],...., A[n-2] are sorted means A[n-1] is also
in proper place.
Thus “ the list of n elements are sorted after (n-1) passes”.
For example, suppose an array A contains 5 elements: 65 32 43 14 26
In first pass, the smallest element is searched by making of 4 comparisons and the
smallest element is 14. Then it is swapped with the first element i.e. 65. After the first pass
the elements are : 14 32 43 65 26.
The complete set of passes in selection sort are:
Algorithm: Selection_Sort( A, n)
// A is an array of n elements
{
for i:= 0 to n-2 do
{
min := A[i]; minpos := i;
for j:= i+1 to n-1 do
if(a[j]< min) then
{ min := a[j] ; minpos := j; }
swap(A[i], A[minpos]);
}
}
Analysis:
It is easiest sorting method. If list is already sorted, only one comparison is made on each
pass so that (n-1) passes requires (n-1) comparisons. Hence the time complexity is O(n)
i.e. Best case.
In worst case i.e if the list is sorted in reverse order ( unsorted) then the total no. Of
comparisons are ,
1 + 2 + 3 + ... + (n-1) = n(n-1)/2 = (n2-n)/2 = O(n2).
So, the Worst Case time complexity is : O(n2)
and the Best Case time complexity is : O(n)