Last active
June 25, 2018 10:51
-
-
Save sudhanshuptl/2694f41241f3c0085c19edbf8d504706 to your computer and use it in GitHub Desktop.
sorting Algorithms in c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#define SIZE 10 | |
void bubble_sort(int values[], int n) | |
{ | |
// BUBBLE SHORT | |
int i,j,temp; | |
for(i=0;i<n;i++){ | |
for(j=0;j<n-1-i;j++){ | |
if(values[j]>values[j+1]){ | |
temp=values[j]; | |
values[j]=values[j+1]; | |
values[j+1]=temp; | |
} | |
} | |
} | |
} | |
int main(){ | |
int i; | |
int arr[SIZE]={5,3,6,2,6,3,5,9,6,7}; | |
bubble_sort(arr,SIZE); | |
for(i=0;i<SIZE;i++){ | |
printf("%d, ",arr[i]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdio.h> | |
#define SIZE 10 | |
void inserstion_sort(int values[], int n) | |
{ | |
// inserstion_sort | |
int ent,index; | |
int i,j,temp; | |
for(i=1;i<n;i++){ | |
for(j=0;j<i;j++){ | |
if(values[j]>values[i]) { | |
temp=values[j]; | |
values[j]=values[i]; | |
values[i]=temp; | |
} | |
} | |
} | |
} | |
int main(){ | |
int i; | |
int arr[SIZE]={5,3,6,2,6,3,5,9,6,1}; | |
inserstion_sort(arr,SIZE); | |
for(i=0;i<SIZE;i++){ | |
printf("%d, ",arr[i]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#define SIZE 10 | |
void print(int arr[]); | |
void mergeSort(int arr[],int base,int end); | |
void merge(int arr[],int base,int mid, int end); | |
int main(){ | |
int arr[SIZE]={5,32,7,2,8,7,9,6,43,65}; | |
print(arr); | |
mergeSort(arr,0,SIZE-1); | |
print(arr); | |
return 0; | |
} | |
void mergeSort(int arr[],int base,int end){ | |
int mid; | |
if (end >base){ | |
mid = (base+(end-1))/2; | |
mergeSort(arr, base, mid); | |
mergeSort(arr, mid+1, end); | |
merge(arr, base, mid, end); | |
} | |
} | |
void merge(int arr[], int l,int m, int r){ | |
int i,j,k,temp; | |
i = l; | |
j = m+1; | |
while(i <= m+2 && j <= r){ | |
if(arr[i] < arr[j]){ | |
i++; | |
} | |
else{ | |
temp = arr[j]; | |
for(k=j-1;k>=i;k--) | |
arr[k+1] = arr[k]; | |
arr[i] = temp; | |
j++; | |
} | |
} | |
} | |
void print(int arr[]){ | |
int i; | |
printf("Printing Data..: "); | |
for(i=0;i<SIZE;i++) | |
printf("%d ,",arr[i]); | |
printf("\n"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Quick Sort | |
*/ | |
#include<stdio.h> | |
#define SIZE 10 | |
void print(int arr[]); | |
void quick_sort(int arr[],int base,int end); | |
int partition(int arr[], int base,int end); | |
void swap(int *a, int *b); | |
int main(){ | |
int arr[SIZE]={66,63,12,88,33,73,99,7,3,44}; | |
print(arr); | |
quick_sort(arr, 0, SIZE-1); | |
print(arr); | |
return 0; | |
} | |
void swap(int *a, int *b){ | |
int temp; | |
temp = *b; | |
*b = *a; | |
*a = temp; | |
} | |
int partition(int arr[], int base,int end){ | |
int pivot=base; | |
int pivot_value = arr[end]; | |
int temp, i; | |
for(i=base;i<=end-1;i++){ | |
if(arr[i] < pivot_value){ | |
//swap | |
swap(&arr[i],&arr[pivot]); | |
pivot++; | |
} | |
} | |
swap(&arr[pivot],&arr[end]); | |
//printf("\n Pivote index : %d, pivote value : %d, %d %d\n",pivot,pivot_value,base,end); | |
//print(arr); | |
return pivot; | |
} | |
void quick_sort(int arr[],int base,int end){ | |
int pivot; | |
if(base >= end) | |
return; | |
pivot = partition(arr, base, end); | |
quick_sort(arr,base,pivot-1); | |
quick_sort(arr,pivot+1,end); | |
} | |
void print(int arr[]){ | |
int i; | |
printf("Printing Current status:\n"); | |
for(i=0;i<SIZE;i++){ | |
printf("%d ,",arr[i]); | |
} | |
printf("\n"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdio.h> | |
#define SIZE 10 | |
void selection_sort(int values[], int n) | |
{ | |
// Selection SORT | |
int min,index; | |
int i,j,temp; | |
for(i=0;i<n;i++){ | |
min=values[i]; | |
index=i; | |
for(j=i;j<n;j++){ | |
if(values[j]<min){ | |
min=values[j]; | |
index=j; | |
} | |
} | |
temp=values[i]; | |
values[i]=min; | |
values[index]=temp; | |
} | |
} | |
int main(){ | |
int i; | |
int arr[SIZE]={5,3,6,2,6,3,5,9,6,5}; | |
selection_sort(arr,SIZE); | |
for(i=0;i<SIZE;i++){ | |
printf("%d, ",arr[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment