DS Programs
DS Programs
DS Programs
Pass-1 2 3 51 23 13
7 2
27 3 23 13
51
2
27 32 2 13
51
3
27 32 23 51 13
Pass-2 2 3 23 13 51
7 2
27 3 2 13 51
2 3
27 23 3
13 51
2
27 23 13
32 51
2 2 13
7 3 32 51
23 2
13 32 51
7
23 13
27 32 51
Pass-4 2
13 27 32 51
3
13 23 27 32 51
Pass-1 77 33 44 55 88 66 22 11
Pass-2 11 33 44 55 88 66 22 77
Pass-3 11 22 44 55 88 66 33 77
Pass-4 11 22 33 55 88 66 44 77
Pass-5 11 22 33 44 88 66 55 77
Pass-6 11 22 33 44 55 66 88 77
Pass-7 11 22 33 44 55 66 88 77
Sathi Durga Devi, Dept of IT
Write a C program to implement Selection sort
#include<stdio.h>
void selectionsort(int a[],int n);
void main()
{
int a[100],i,n;
printf("\n enter the size of the array");
scanf("%d",&n);
printf("enter array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionsort(a,n);
}//main
12
6 10 24 36
12
6 10 24 3
6
12
sorted unsorted
mergesort(a,0,3) mergesort(a,4,7)
8 3 2 9 7 1 5 4
mergsort(a,0,0)
ms(a,1,1) ms(a,2,2) ms(a,3,3) ms(a,4,4) ms(a,5,5) ms(a,6,6) ms(a,7,7)
8 3 2 9 7 1 5 4
merge(a,0,3,1) merge(a,4,7,5)
2 3 8 9 1 4 5 7
merge(a,0,7,3)
Sathi Durga Devi, Dept of IT
1 2 3 4 5 7 8 9
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
int x[20],size,i;
printf("\nEnter size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("\nSorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
getch();
return 0;
}
Sathi Durga Devi, Dept of IT
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j]; x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
Sathi Durga Devi, Dept of IT
}
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 4 7 20 10 30 40 50 60 80 100
i j
7 20 10 30 40 50 60 80 100
7 20 10 30 40 50 60 80 100