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

Sorting Algorithms in C: Code Examples

The document provides implementations of four sorting algorithms: Insertion Sort, Selection Sort, and Quick Sort in C programming. Each algorithm is accompanied by sample code and example input/output to demonstrate its functionality. The Insertion Sort and Selection Sort examples include user input for the number of elements and the elements themselves, while the Quick Sort example showcases a recursive approach to sorting.

Uploaded by

Baba Waris
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)
22 views4 pages

Sorting Algorithms in C: Code Examples

The document provides implementations of four sorting algorithms: Insertion Sort, Selection Sort, and Quick Sort in C programming. Each algorithm is accompanied by sample code and example input/output to demonstrate its functionality. The Insertion Sort and Selection Sort examples include user input for the number of elements and the elements themselves, while the Quick Sort example showcases a recursive approach to sorting.

Uploaded by

Baba Waris
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

16.

Implement the sorting algorithms


a. Insertion sort b. Exchange sort c. Selection sort d. Partitioning sort.

Insertion sort

#include <stdio.h>
int main()
{
int n, i, j, temp;
int arr[64];

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);


for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1 ; i <= n - 1; i++)
{
j = i;
while ( j > 0 && arr[j-1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i <= n - 1; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
Output:
Enter number of elements
4
Enter 4 integers
5412
Sorted list in ascending order:
1
2
4
5

Sorted list in ascending order:


1
2
3
5

Selection sort
#include<stdio.h>
int main(){

int i, j, count, temp, number[25];

printf("How many numbers u are going to enter?: ");


scanf("%d",&count);

printf("Enter %d elements: ", count);

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

for(i=0;i<count;i++){
for(j=i+1;j<count;j++){
if(number[i]>number[j]){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}

printf("Sorted elements: ");


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

return 0;
}

How many numbers u are going to enter?: 4


Enter 4 elements: 2 3 1 5
Sorted elements: 1 2 3 5

Quick sort
#include <stdio.h>
void quicksort (int [], int, int);

int main()
{
int list[50];
int size, i;

printf("Enter the number of elements: ");


scanf("%d", &size);
printf("Enter the elements to be sorted:\n");
for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
quicksort(list, 0, size - 1);
printf("After applying quick sort\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");

return 0;
}
void quicksort(int list[], int low, int high)
{
int pivot, i, j, temp;
if (low < high)
{
pivot = low;
i = low;
j = high;
while (i < j)
{
while (list[i] <= list[pivot] && i <= high)
{
i++;
}
while (list[j] > list[pivot] && j >= low)
{
j--;
}
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, low, j - 1);
quicksort(list, j + 1, high);
}
}

Enter the number of elements: 4


Enter the elements to be sorted:
2316
After applying quick sort
1236

You might also like