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

All Sorting Programs Full Structured

The document contains C programs for four sorting algorithms: Insertion Sort, Bubble Sort, Selection Sort, and Merge Sort. Each sorting method includes a function to print the array, the sorting logic, and a main function that handles user input and displays the sorted array. The code is structured with comments for clarity and understanding.

Uploaded by

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

All Sorting Programs Full Structured

The document contains C programs for four sorting algorithms: Insertion Sort, Bubble Sort, Selection Sort, and Merge Sort. Each sorting method includes a function to print the array, the sorting logic, and a main function that handles user input and displays the sorted array. The code is structured with comments for clarity and understanding.

Uploaded by

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

All Sorting Programs in C (Full Structured with Input,

Function Calls & Comments)


================ INSERTION SORT =================

#include<stdio.h>

// Function to print array


void pf(int a[],int n){
for(int i=0;i<n;i++){ // loop to print elements
printf("%d ",a[i]);
}
printf("\n");
}

// Insertion sort function


void insertionsort(int a[],int n){
int i,j,temp;

for(i=1;i<n;i++){ // loop for passes


j=i;

while(j>0 && a[j]<a[j-1]){ // place element at correct position


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

int main(){
int a[100];
int i,n;

printf("enter no of ele");
scanf("%d",&n);

for(i=0;i<n;i++){ // input array


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

pf(a,n); // print before sorting


insertionsort(a,n); // function call
pf(a,n); // print after sorting

return 0;
}

================ BUBBLE SORT =================

#include<stdio.h>

void pf(int a[],int n){


for(int i=0;i<n;i++){
printf("%d ",a[i]);
}
printf("\n");
}

void bubblesort(int a[],int n){


int i,j,temp;

for(i=0;i<n-1;i++){ // passes
for(j=0;j<n-i-1;j++){ // compare adjacent elements
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

int main(){
int a[100];
int i,n;

printf("enter no of ele");
scanf("%d",&n);

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

pf(a,n);
bubblesort(a,n);
pf(a,n);

return 0;
}

================ SELECTION SORT =================

#include<stdio.h>

void pf(int a[],int n){


for(int i=0;i<n;i++){
printf("%d ",a[i]);
}
printf("\n");
}

void selectionsort(int a[],int n){


int i,j,min,temp;

for(i=0;i<n-1;i++){ // select position


min=i;

for(j=i+1;j<n;j++){ // find minimum


if(a[j]<a[min]){
min=j;
}
}

temp=a[min];
a[min]=a[i];
a[i]=temp;
}
}

int main(){
int a[100];
int i,n;

printf("enter no of ele");
scanf("%d",&n);

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

pf(a,n);
selectionsort(a,n);
pf(a,n);

return 0;
}
================ MERGE SORT =================

#include<stdio.h>

void pf(int a[],int n){


for(int i=0;i<n;i++){
printf("%d ",a[i]);
}
printf("\n");
}

void merge(int a[],int low,int mid,int high){


int i,j,k,B[100];
i=low;
j=mid+1;
k=low;

while(i<=mid && j<=high){


if(a[i]<a[j]){
B[k]=a[i];
i++;
k++;
}
else{
B[k]=a[j];
j++;
k++;
}
}

while(i<=mid){
B[k]=a[i];
k++;
i++;
}

while(j<=high){
B[k]=a[j];
k++;
j++;
}

for(int i=low;i<=high;i++){
a[i]=B[i];
}
}

int mergesort(int a[],int low,int high){


if(low>=high)
return 0;
else{
int mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}

int main(){
int a[100];
int i,n;

printf("enter no of ele");
scanf("%d",&n);

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

pf(a,n);
mergesort(a,0,n-1);
pf(a,n);
return 0;
}

You might also like