Bubble Sort

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

BUBBLE SORTING

#include<stdio.h>

#include<conio.h>

void main()

int array[100], n, i, j, swap;

printf("Enter number of elementsn");

scanf("%d", &n);

printf("Enter %d Numbers:n", n);

for(i = 0; i < n; i++)

scanf("%d", &array[i]);

for(i = 0 ; i < n - 1; i++)

for(j = 0 ; j < n-i-1; j++)

if(array[j] > array[j+1])

swap=array[j];

array[j]=array[j+1];

array[j+1]=swap;

}
}

printf("Sorted Array:n");

for(i = 0; i < n; i++)

printf("%dn", array[i]);

getch();

SELECTION SORTING
#include<stdio.h>
#include<conio.h>

void main(){

int size,i,j,temp,list[100];
clrscr();

printf("Enter the size of the List: ");


scanf("%d",&size);

printf("Enter %d integer values: ",size);


for(i=0; i<size; i++)
scanf("%d",&list[i]);

//Selection sort logic

for(i=0; i<size; i++){


for(j=i+1; j<size; j++){
if(list[i] > list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}

printf("List after sorting is: ");


for(i=0; i<size; i++)
printf(" %d",list[i]);

getch();
}
SELECTION SORT

include <stdio.h>

include<conio.h>

int main()

int a[100], n, i, j, position, swap;

printf("Enter number of elementsn");

scanf("%d", &n);

printf("Enter %d Numbersn", n);

for (i = 0; i < n; i++)

scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)

position=i;

for(j = i + 1; j < n; j++)

if(a[position] > a[j])

position=j;

if(position != i)

swap=a[i];

a[i]=a[position];

a[position=swap;

printf("Sorted Array:n");

for(i = 0; i < n; i++)

printf("%dn", a[i]);

return 0;

You might also like