Untitled

 avatar
irfann
plain_text
7 months ago
2.1 kB
0
Indexable
Never
Bubble Sort Program

#include<stdio.h>
#include<conio.h>
void main()
{
int a[8],i,temp,j,k,n;
clrscr();
printf("\n Enter size of array:");
scanf("%d",&n);
printf("\n Enter array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Array elements are:");
for(i=0;i<n;i++)
{
printf("\n %d",a[i]);
}
for(i=1;i<n;i++)//passes
17
{
for(j=0;j<n
-i;j++)//inner element
{
if(a[j]>a[j+1]) {
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp; }}
//print resultant array
printf("
\n Sorted array is:");
for(k=0;k<n;k++) {
printf("
\n%d",a[k]);
}}
getch(); }

SELECTION SORT

#include <stdio.h>
#include <conio.h>
void main() {
 int a[8], i, temp, j, k, n;
 clrscr();
 printf("
\n Enter size of array:");
 scanf("%d", &n);
 printf("
\n Enter array elements:");
 for (i = 0; i < n; i++) {
 scanf("%d", &a[i]);
 
}
 printf("
\n Array elements are:");
 for (i = 0; i < n; i++) {
 printf("
\n %d", a[i]);
 
}
 // Selection Sort
 for (i = 0; i < n 
- 1; i++) {
 int min_index = i;
 for (j = i + 1; j < n; j++) {
 if (a[j] < a[min_index]) {
 min_index = j;
 
}
18
 
}
 if (min_index != i) {
 temp = a[i];
 a[i] = a[min_index];
 a[min_index] = temp;
 
}
 // Print array after each pass
 printf("
\n Sorted array after pass %d is:", i + 1);
 for (k = 0; k < n; k++) {
 printf("
\n %d", a[k]);
 
}
 
}
 getch(); }

INSERTION SORT PROGRAM

#include <stdio.h>
#include <conio.h>
void main() {
 int a[8], i, temp, j, k, n;
 clrscr();
 printf("
\n Enter size of array:");
 scanf("%d", &n);
 printf("
\n Enter array elements:");
 for (i = 0; i < n; i++) {
 scanf("%d", &a[i]);
 
}
 printf("
\n Array elements are:");
 for (i = 0; i < n; i++) {
 printf("
\n %d", a[i]);
 
}
 // Insertion Sort
 for (i = 1; i < n; i++) {
 temp = a[i];
 j = i 
- 1;
 while (j >= 0 && a[j] > temp) {
 a[j + 1] = a[j];
 
j--
;
 
}
 a[j + 1] = temp;
 // Print array after each pass
 printf("\n Sorted array after pass %d is:", i);
 for (k = 0; k < n; k++) {
 printf("\n %d", a[k]);
 }
 }
 getch();
}

Leave a Comment