0% found this document useful (0 votes)
14 views

Sorting Using C

The document discusses different sorting algorithms including bubble sort, insertion sort, selection sort, merge sort, quick sort, and heap sort. Code examples in C are provided to demonstrate how each algorithm works.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Sorting Using C

The document discusses different sorting algorithms including bubble sort, insertion sort, selection sort, merge sort, quick sort, and heap sort. Code examples in C are provided to demonstrate how each algorithm works.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Bubble Sort

#include <stdio.h>
int main()
{
int array[100], n, i, j, swap;
printf("enter number of elements\n");
scanf("%d", &n);
printf("enter %d integers\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 list in ascending order:\n");
for (i = 0; i < n; i++)
printf("%d\n", array[i]);
return 0;
}

Output

enter number of elements


5
enter 5 integers
43
6
432
87
41
sorted list in ascending order:
6
41
43
87
432

Insertion Sort
#include <stdio.h>
int main()
{
int n, a[100], i, j, temp;
printf("enter number of elements\n");
scanf("%d", &n);
printf("enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 1; i < n; i++)
{
temp = a[i];
j = i - 1;
while (j >= 0 && temp < a[j])
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = temp;
}
printf("sorted element:\n");
for (i = 0; i < n; i++)
{
printf("\n%d", a[i]);
}
return 0;
}

Output
enter number of elements

enter 5 integers
54

87

54

32

sorted element:

32

54

54

87

Selection Sort
#include <stdio.h>
void selection(int a[], int n)
{
int i, j, min, temp;
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i + 1; j < n; j++)
{
if (a[j] < a[min])
min = j;
temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
}
int main()
{
int a[40], n, i;
printf("enter the size of array\n");
scanf("%d", &n);
printf("enter elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d\n", &a[i]);
}
selection(a, n);
printf("sorted elements:\n");
for (i = 0; i < n; i++)
{
printf("\n\t\n%d", a[i]);
}
return 0;
}

Output
enter the size of array

enter elements:

54

78

54

23

21

sorted elements:

23

54

54

78

Merge Sort
#include <stdio.h>
void mergesort();
void merge();
int main()
{
int a[40], n, i;
printf("enter the size of array\n");
scanf("%d", &n);
printf("enter elements:\n\n");
for (i = 0; i < n; i++)
scanf("\n%d", &a[i]);
mergesort(a, 0, n - 1);
printf("sorted element are:\n\n");
for (i = 0; i < n; i++)
printf("\n\n%d", a[i]);
return 0;
}
void mergesort(int a[], int first, int last)
{
int mid;
if (first < last)
{
mid = (first + last) / 2;
mergesort(a, first, mid);
mergesort(a, mid + 1, last);
merge(a, first, mid, last);
}
}
void merge(int a[], int first, int mid, int last)
{
int b[50];
int i, j, k;
i = first;
j = mid + 1;
k = first;
while (i <= mid && j <= last)
{
if (a[i] <= a[j])
b[k++] = a[i++];
else
b[k++] = a[j++];
}
if (i > mid)
{
while (j <= last)
{
b[k++] = a[j++];
}
}
else
{
while (i <= mid)
{
b[k++] = a[i++];
}
}
for (i = first; i <= last; i++)
{
a[i] = b[i];
}
}

Output
enter the size of array

enter elements:

34

32

67

sorted element are:

32

34

67

Quick Sort
#include <stdio.h>
void quicksort(int a[40], int first, int last)
{
int i, j, pivot, temp;
if (first < last)
{
pivot = first;
i = first;
j = last;
while (i < j)
{
while (a[i] <= a[pivot] && i < last)
i++;
while (a[j] > a[pivot])
j--;
if (i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[pivot];
a[pivot] = a[j];
a[j] = temp;
quicksort(a, 0, j - 1);
quicksort(a, j + 1, last);
}
}
int main()
{
int a[40], n, i;
printf("enter the size of array:\n");
scanf("%d", &n);
printf("enter elements:\n");
for (i = 0; i < n; i++)
scanf("%d\n", &a[i]);
quicksort(a, 0, n - 1);
printf("sorted elements are:\n");
for (i = 0; i < n; i++)
printf("\t\n%d", a[i]);
return 0;
}

Output
enter number of elements

enter 5 integers
54

87

54

32

sorted element:

32

54

54

87

Heap Sort
#include <stdio.h>
#include <conio.h>
void maxheapify(int a[], int, int);
void maxheap(int a[], int beg, int end)
{
int i;
for (i = end / 2; i >= beg; i--)
maxheapify(a, i, end);
}

void maxheapify(int a[], int f, int size)


{
int max = f, l = f * 2, r = f * 2 + 1, t;
if (l <= size && a[l] > a[max])
max = l;
if (r <= size && a[r] > a[max])
max = r;
if (f != max)
{
t = a[f];
a[f] = a[max];
a[max] = t;
maxheapify(a, max, size);
}
}
void heapsort(int a[], int size)
{
int i, t;
for (i = size; i >= 2; i--)
{
t = a[1];
a[1] = a[i];
a[i] = t;
maxheapify(a, 1, i - 1);
}
}

void main()
{
int a[10], i;
printf(" enter the elements:\n");
for (i = 1; i < 10; i++)
scanf("%d\n\n", &a[i]);
maxheap(a, 1, 9);
heapsort(a, 9);
printf("\n....SORTED ELEMENTS....\n");
for (i = 1; i < 10; i++)
printf("%d\n\n", a[i]);
}

Output
enter the elements:
55
87
76
9
43
43
32
65
21
65

....SORTED ELEMENTS....
9

21
32

43

43

55

65

76

87

You might also like