0% found this document useful (0 votes)
5 views12 pages

Sorting Algorithms in C: Selection, Quick, Bubble, Insertion, Merge

The document contains implementations of several sorting algorithms in C, including selection sort, quick sort, bubble sort, insertion sort, and merge sort. Each algorithm is presented with a main function that allows user input for an array size and its elements, followed by sorting and displaying the sorted array. Additionally, a linear search implementation is provided to find the index of a target element in an array.

Uploaded by

dineshs143u
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)
5 views12 pages

Sorting Algorithms in C: Selection, Quick, Bubble, Insertion, Merge

The document contains implementations of several sorting algorithms in C, including selection sort, quick sort, bubble sort, insertion sort, and merge sort. Each algorithm is presented with a main function that allows user input for an array size and its elements, followed by sorting and displaying the sorted array. Additionally, a linear search implementation is provided to find the index of a target element in an array.

Uploaded by

dineshs143u
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

Selection sort

#include <stdio.h>

void selectionSort(int* arr, int n) {

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

int min_idx = i;

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

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

min_idx = j;

int temp = arr[i];

arr[i] = arr[min_idx];

arr[min_idx] = temp;

int main() {

int size;

printf("Enter the Size of array");

scanf("%d",&size);

int arr[size];

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

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

selectionSort(arr, size);
for (int i = 0; i < size; i++) {

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

return 0;

Qsort
#include<stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int part(int *arr,int low,int high){

int pivot=arr[high];

int j=low-1;

for(int i=low;i<high;i++){

if(arr[i]<pivot){

j++;

swap(&arr[j], &arr[i]);

swap(&arr[j + 1], &arr[high]);

return j+1;

void quickSort(int* arr,int low,int high){

if(low<high){

int index=part(arr,low,high);

quickSort(arr,low,index-1);

quickSort(arr,index+1,high);

}
int main() {

int size;

printf("Enter the Size of array ");

scanf("%d",&size);

int arr[size];

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

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

quickSort(arr,0, size-1);

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

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

return 0;

Bubble sort
#include <stdio.h>

void bubbleSort(int* arr, int n) {

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

int flag=0;

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

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

flag=1;

int temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

if(flag==0){

break;

int main() {

int size;

printf("Enter the Size of array ");

scanf("%d",&size);

int arr[size];

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

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

bubbleSort(arr, size);
for (int i = 0; i < size; i++) {

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

return 0;

Insertion sort
#include<stdio.h>

void insertionSort(int *arr,int n){

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

int key=arr[i];

int j=i-1;

while(j>=0 && arr[j]>key){

arr[j+1]=arr[j];

j--;

arr[j+1]=key;

int main() {

int size;

printf("Enter the Size of array ");

scanf("%d",&size);

int arr[size];

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

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

insertionSort(arr, size);

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

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

return 0;

Merg sort
#include<stdio.h>

void merge(int *arr,int left,int right,int mid){

int i,j,k;

int n1=mid-left+1;

int n2=right-mid;

int leftArray[n1],rightArray[n2];

for(i=0;i<mid-left+1;i++){

leftArray[i]=arr[left+i];

for(j=0;j<right-mid;j++){

rightArray[j]=arr[mid+1+j];

i=0,j=0,k=left;

while(i<n1 && j<n2){

if(leftArray[i]<rightArray[j]){

arr[k]=leftArray[i];

i++;

}else{

arr[k]=rightArray[j];

j++;

k++;

while(i<n1){

arr[k]=leftArray[i];

k++;

i++;

while(j<n2){

arr[k]=rightArray[j];

k++;
j++;

void mergeSort(int *arr,int left,int right){

if(left<right){

int mid=left+(right-left)/2;

mergeSort(arr,left,mid);

mergeSort(arr,mid+1,right);

merge(arr,left,right,mid);

int main(){

int size;

printf("Enter the size: ");

scanf("%d",&size);

int arr[size];

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

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

mergeSort(arr,0,size-1);

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

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

}
Quick sort

#include<stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int part(int *arr,int low,int high){

int pivot=arr[high];

int j=low-1;

for(int i=low;i<high;i++){

if(arr[i]<pivot){

j++;

swap(&arr[j], &arr[i]);

swap(&arr[j + 1], &arr[high]);

return j+1;

void quickSort(int* arr,int low,int high){

if(low<high){

int index=part(arr,low,high);

quickSort(arr,low,index-1);

quickSort(arr,index+1,high);

}
int main() {

int size;

printf("Enter the Size of array ");

scanf("%d",&size);

int arr[size];

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

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

quickSort(arr,0, size-1);

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

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

return 0;

}
Linear search

#include<stdio.h>

int main(){

int size,target;

printf("Enter the Size: ");

scanf("%d",&size);

int arr[size];

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

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

scanf("%d",&target);

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

if (arr[i]==target)

printf("%d",i);

return -1;

You might also like