Implementation of different sorting algorithm for sorting N numbers in C language.

avatar

70d6f4e10e2badb5ef394f00c17ad2bc1c14f6e7.jpg

In this post i will be showing different types of sorting algorithm in C language.

Aim: In this post i will be showing different types of sorting algorithm in C language.

Algorithm:

For Ascending:

  1. Input the total number of elements of the array
  2. Input the elements of the array
  3. For all elements in the array
  4. Set min to the first location
  5. If current element is less than min
  6. swap the first location with the minimum value in the array
  7. Increment min to the next element
  8. Repeat the process until we get a sorted array.

For Descending:

  1. For all elements in the array
  2. Set min to the first location
  3. If current element is greater than min
  4. swap the first location with the minimum value in the array
  5. Increment min to the next element
  6. Repeat the process until we get a sorted array.

Code:

#include <stdio.h>
int main(){
int array[10],n,temp,min;
printf("------------------------\n");
printf("Enter the number of elements of the array:");
scanf("%d",&n);
printf("the elements of the array:");
for(int i = 0; i<n; i++){
scanf("%d",&array[i]);
}
for(int i = 0; i < n - 1; i++){
min=i;
for(int j = i + 1; j < n; j++){
if(array[min] > array[j])
min=j;
}
if(min != i){
temp=array[i];
array[i]=array[min];
array[min]=temp;
}
}
printf("The elements of the array in ascending order are: \n");
for(int i = 0; i<n; i++){
printf("%d\n",array[i]);
}
for(int i = 0; i < n - 1; i++){
min=i;
for(int j = i + 1; j < n; j++){
if(array[min] < array[j])
min=j;
}
if(min != i){
temp=array[i];
array[i]=array[min];
array[min]=temp;
}
}
printf("The elements of the array in descending order are: \n");
for(int i = 0; i<n; i++){
printf("%d\n",array[i]);
}
}

Aim: To develop C programming for sorting N elements using Bubble sort algorithms.

Algorithm:

**For Ascending: **

  1. Input the total number of elements of the array
  2. Input the elements of the array
  3. For all elements in the array
  4. If current element is greater than next element
  5. Swap the elements
  6. Repeat the process until we get a sorted array

**For descending: **

  1. For all elements in the array
  2. If left element is less than right element
  3. Swap the elements
  4. Repeat the process until we get a sorted array

Code:

#include <stdio.h>
int main(){
int array[10],n,temp;
printf("------------------------\n");
printf("Enter the number of elements of the array:");
scanf("%d",&n);
printf("the elements of the array:");
for(int i = 0; i<n; i++){
scanf("%d",&array[i]);
}
for(int i =0; i<n-1;i++){
for(int j = 0; j<n-1;j++){
if(array[j] > array[j+1]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("The elements of the array in ascending order are: \n");
for(int i = 0; i<n; i++){
printf("%d\n",array[i]);
}
for(int i =0; i<n-1;i++){
for(int j = 0; j<n-1;j++){
if(array[j] < array[j+1]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("The elements of the array in descending order are: \n");
for(int i = 0; i<n; i++){
printf("%d\n",array[i]);
}
}

Aim: To develop C programming for sorting N elements using insertion sort algorithms.

Algorithm:

For Ascending:

  1. Input the total number of elements of the array
  2. Input the elements of the array
  3. Loop 0 to n
  4. Set temporary element as the first element of the unsorted array
  5. Loop while j greater than or equal to i-1 and current index is
    greater than temporary
  6. Move the current element to +1 index
  7. Exit while loop
  8. Insert the temporary value to current index
  9. Repeat the process until we get a sorted array

For descending:

  1. Loop 0 to n
  2. Set temporary element as the first element of the unsorted array
  3. Loop while j greater than or equal to i-1 and current index is less
    than temporary
  4. Move the current element to +1 index
  5. Exit while loop
  6. Insert the temporary value to current index
  7. Repeat the process until we get a sorted array

Code:

#include <stdio.h>
int main(){
int temp,array[10],n,j;
printf("------------------------\n");
printf("Enter the number of elements of the array:");
scanf("%d",&n);
printf("the elements of the array:");
for(int i = 0; i<n; i++){
scanf("%d",&array[i]);
}
for(int i = 1; i<n; i++){
temp = array[i];
j = i-1;
while(j>=0 && array[j] > temp){
array[j+1] = array[j];
--j;
}
array[j + 1] = temp;
}
printf("The elements of the array in ascending order are: \n");
for(int i = 0; i<n; i++){
printf("%d\n",array[i]);
}
for(int i = 1; i<n; i++){
temp = array[i];
j = i-1;
while(j>=0 && array[j] < temp){
array[j+1] = array[j];
--j;
}
array[j + 1] = temp;
}
printf("The elements of the array in descending order are: \n");
for(int i = 0; i<n; i++){
printf("%d\n",array[i]);
}
}

Aim: To develop C programming for sorting N elements using Merge sort algorithms.

Algorithm:

  1. Get the array's input N -number of elements in the array
  2. Get the input elements
  3. Return if there is just one entry in the list, that has already been sorted.
  4. split the list recursively into two parts until it can no longer be divided
  5. In sorted order, merge the smaller lists into a new list.
  6. Until we get a sorted array repeat the process
  7. Print the result.

Code:
#include<stdio.h>
void quicksort(int number[50],int first,int last){
int i, j, pivot, temp;

if(first<last){
pivot=first;
i=first;
j=last;

  while(i<j){
     while(number[i]<=number[pivot]&&i<last)
        i++;
     while(number[j]>number[pivot])
        j--;
     if(i<j){
        temp=number[i];
        number[i]=number[j];
        number[j]=temp;
     }
  }

  temp=number[pivot];
  number[pivot]=number[j];
  number[j]=temp;
  quicksort(number,first,j-1);
  quicksort(number,j+1,last);

}
}

int main(){
int i, count, number[50];
printf("20BCE2935\n");
printf("------------------------\n");
printf("enter the numbers of elements:\n ");
scanf("%d",&count);

printf("Enter %d elements:\n ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);

quicksort(number,0,count-1);

printf("Order of Sorted elements:\n ");
for(i=0;i<count;i++)
printf(" %d\n",number[i]);

return 0;
}

Aim: To develop C programming for sorting N elements using Quick sort algorithms.

Algorithm:

  1. Get the input N -number of elements in the array
  2. Get the input elements
  3. A pivot element is used to split an array into subarrays (element selected from the array).
    The pivot element should be positioned so that items less than pivot are positioned on the left side of the pivot and elements bigger than pivot are kept on the right side of the pivot when splitting the array.
  4. The same approach is used to split the left and right subarrays. This method is repeated until each subarray has just one element.
  5. The elements have already been sorted at this stage. The elements are finally combined to form a sorted array.
  6. Print the result.

Code:
#include<stdio.h>
void quicksort(int number[50],int first,int last){
int i, j, pivot, temp;

if(first<last){
pivot=first;
i=first;
j=last;

  while(i<j){
     while(number[i]<=number[pivot]&&i<last)
        i++;
     while(number[j]>number[pivot])
        j--;
     if(i<j){
        temp=number[i];
        number[i]=number[j];
        number[j]=temp;
     }
  }

  temp=number[pivot];
  number[pivot]=number[j];
  number[j]=temp;
  quicksort(number,first,j-1);
  quicksort(number,j+1,last);

}
}

int main(){
int i, count, number[50];
printf("------------------------\n");
printf("enter the numbers of elements:\n ");
scanf("%d",&count);

printf("Enter %d elements:\n ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);

quicksort(number,0,count-1);

printf("Order of Sorted elements:\n ");
for(i=0;i<count;i++)
printf(" %d\n",number[i]);

return 0;
}



0
0
0.000
1 comments