0% found this document useful (0 votes)
23 views4 pages

Sorting Algorithms in C: Insertion, Selection, Bubble

Uploaded by

dhanushd10072006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Sorting Algorithms in C: Insertion, Selection, Bubble

Uploaded by

dhanushd10072006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// Insertion Sort

#include <stdio.h>

void insFunc(int a[],int n)

int i,j,temp;

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

for(j=i;j>0;j--)

if(a[j-1]>a[j])

temp=a[j];

a[j]=a[j-1];

a[j-1]=temp;

int main()

int a[10],n,i;

printf("\n Enter the total No. of Elements : ");

scanf("%d",&n);

printf("\n Please Enter the Array Elements : ");

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

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

insFunc(a,n);

printf("\n Output : ");

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

printf("%d\t",a[i]);
}

printf("\n");

return 0;

// Selection Sort Program

#include <stdio.h>

void selectionSort(int arr[], int n) {

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

int min = i;

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

if (arr[j] < arr[min]) {

min = j;

int temp = arr[i];

arr[i] = arr[min];

arr[min] = temp;

int main() {

int arr[10],n,i;

printf("\n Enter the total No. of Elements : ");

scanf("%d",&n);

printf("\n Please Enter the Array Elements : ");

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

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

selectionSort(arr, n);

printf("Sorted array: ");

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


printf("%d ", arr[i]);

return 0;

// Bubble Sort

#include <stdio.h>

void swap(int arr[], int i, int j)

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

void bubbleSort(int arr[], int n)

int i, j;

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

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

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

swap(arr, j, j + 1);

int main() {

int arr[10],n,i;

printf("\n Enter the total No. of Elements : ");

scanf("%d",&n);

printf("\n Please Enter the Array Elements : ");


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

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

printf("Unsorted array: ");

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

printf("%d ", arr[i]);

printf("\n");

bubbleSort(arr, n);

printf("Sorted array: ");

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

printf("%d ", arr[i]);

printf("\n");

return 0;

You might also like