Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- #include<time.h>
- //Bubble Sort Function
- void bubble_sort(int Array[], int n){
- int i,j,temp;
- for(i=0; i<n; i++){
- for(j=0; j<n-i; j++){
- if(Array[j] > Array[j+1]){
- temp = Array[j];
- Array[j] = Array[j+1];
- Array[j+1] = temp;
- }
- }
- }
- }
- //Selecton Sort Function
- void selection_sort(int Array[], int n){
- int i,j,temp,small_index;
- for(i=0; i<n; i++){
- small_index = i;
- for(j=i+1; j<n; j++){
- if(Array[j] < Array[small_index]){
- small_index = j;
- }
- }
- temp = Array[i];
- Array[i]= Array[small_index];
- Array[small_index] = temp;
- }
- }
- //Insertion Sort function
- void insertion_sort(int Array[], int n){
- int i,j,temp;
- for(i=0; i<n; i++){
- j = i;
- while(j > 0 && Array[j-1] > Array[j]){
- temp = Array[j];
- Array[j] = Array[j-1];
- Array[j-1] = temp;
- j--;
- }
- }
- }
- //Merge Sort function
- void merge(int Array[], int first, int middle, int last){
- int temp[first+last+1];
- int i,j,k;
- i = first;
- j = middle+1;
- k = first; /
- while(i<=middle && j<=last){
- if(Array[i] <= Array[j]){
- temp[k] = Array[i];
- i++;
- k++;
- }
- else{
- temp[k] = Array[j];
- j++;
- k++;
- }
- }
- while(i<=middle){
- temp[k] = Array[i];
- i++;
- k++;
- }
- while(j<=last){
- temp[k] = Array[j];
- k++;
- }
- for(int a=first; a<=last; a++){
- Array[a] = temp[a];
- }
- }
- void merge_sort(int Array[], int first, int last){
- if(first < last){
- int middle = (first+last) / 2;
- merge_sort(Array,first,middle);
- merge_sort(Array,middle+1,last);
- merge(Array,first,middle,last);
- }
- }
- //Quick Sort
- int partition(int Array[], int start, int end){
- int pivot, i, j, temp;
- pivot = Array[end];
- for(i=start-1,j=start; j<end; j++){
- if(Array[j] < pivot){
- i++;
- temp = Array[j];
- Array[j] = Array[i];
- Array[i] = temp;
- }
- }
- temp = Array[end];
- Array[end] = Array[i+1];
- Array[i+1] = temp;
- return i+1;
- }
- void quick_sort(int Array[], int start, int end){
- if(start>=end){
- return;
- }
- int p = partition(Array, start, end);
- quick_sort(Array, start, p-1);
- quick_sort(Array, p+1, end);
- }
- //Main Function
- int main(){
- int A[10000],size, i, key;
- //Input Array Part
- srand(time(0));
- printf("\nEnter the size of the array : "); scanf("%d",&size);
- for(i=0; i<size; i++){
- A[i] = rand()%150;
- }
- printf("\nArray elements are : \n");
- for(i=0; i<size; i++){
- printf("%d\t", A[i]);
- }
- /*
- //call any of these function to sort the array
- //bubble_sort(A,size);//calling the bubble sort function
- //selection_sort(A,size);//calling the selection sort function
- //insertion_sort(A,size);//calling the insertion sort function
- //merge_sort(A,0,size-1);
- //quick_sort(A, 0, size-1);
- printf("\nAfter sorting the array is : \n");
- for(i=0; i<size; i++){
- printf("%d\t", A[i]);
- }
- */
- printf("\n");
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement