C program to sort an array in ascending and descending order
Sort the array in C
Here, on this page, we will discuss the program to sort the array in the C programming language. We are given an array and need to sort it in ascending and descending order. Methods for sorting of array in C, We will discuss various algorithms to sort the given input array.
Different methods Discussed in this page are :
- Method 1 : Using Selection Sort
- Method 2 : Using Insertion Sort
- Method 3 : Using Bubble Sort
Method 1 :
In this method we will use Selection Sorting Technique to sort the given input array. You can click on the button given below to understand the algorithm for selection sorting technique.
Method 1 : Code in C
// C program for implementation of selection sort #include <stdio.h> void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp; } void selectionSort(int array[], int size) { int i, j, min_idx; // Loop to iterate on array for (i = 0; i < size-1; i++) { // Here we try to find the min element in array min_idx = i; for (j = i+1; j < size; j++) { if (array[j] < array[min_idx]) min_idx = j; } // Here we interchange the min element with first one swap(&array[min_idx], &array[i]); } } /* Display function to print values */ void display(int array[], int size) { int i; for (i=0; i < size; i++) { printf("%d ",array[i]); } printf("\n"); } // The main function to drive other functions int main() { int array[] = {50, 30, 10, 90, 80, 20, 40, 70}; int size = sizeof(array)/sizeof(array[0]); selectionSort(array, size); display(array, size); return 0; }
Output
10 20 30 40 50 70 80 90
Method 2 :
In this method we will use Insertion Sorting Technique to sort the given input array. You can click on the button given below to understand the algorithm for insertion sorting technique.
Method 2 : Code in C
#include<stdio.h> // Here we are implementing Insertion sort void insertionSort(int array[], int size) { int i, target, j; for (i = 1; i < size; i++) { target = array[i]; j = i - 1; /* Here the elements in b/w arrary[0 to i-1] which are greater than target are moved ahead by 1 position each*/ while (j >= 0 && array[j] > target) { array[j + 1] = array[j]; j = j - 1; } array[j + 1] = target; } } /* Function to print array */ void display(int arr[], int size) { int i; for (i=0; i < size; i++) printf("%d ", arr[i]); printf("\n"); } // Main function to run the program int main() { int array[] = {50, 30, 10, 90, 80, 20, 40, 70}; int size = sizeof(array)/sizeof(array[0]); insertionSort(array, size); display(array, size); return 0; }
Output
10 20 30 40 50 60 70 80 90
Method 3 :
In this method we will use Bubble Sorting Technique to sort the given input array. You can click on the button given below to understand the algorithm for bubble sorting technique.
Method 3 : Code in C
#include<stdio.h> void swap(int *var1, int *var2) { int temp = *var1; *var1 = *var2; *var2 = temp; } // Here we are implementing bubbleSort void bubbleSort(int arr[], int n) { int i, j; for (i = 0; i < n-1; i++){ // Since, after each iteration righmost i elements are sorted for (j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) swap(&arr[j], &arr[j+1]); } } /* Function to print array */ void display(int arr[], int size) { int i; for (i=0; i < size; i++) printf("%d ", arr[i]); printf("\n"); } // Main function to run the program int main() { int array[] = {50, 30, 10, 90, 80, 20, 40, 70}; int size = sizeof(array)/sizeof(array[0]); bubbleSort(array, size); display(array, size); return 0; }
Output
10 20 30 40 50 60 70 80 90
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Important Codes related to Arrays
- Find Smallest Element in an Array : C | C++ | Java | Python
- Find Second Smallest Element in an Array : C | C++ | Java | Python
- Find Largest element in an array : C | C++ | Java | Python
- Find the Smallest and largest element in an array : C | C++ | Java | Python
- Calculate the sum of elements in an array : C | C++ | Java | Python
- Reverse an Array : C | C++ | Java | Python
- Sort first half in ascending order and second half in descending : C | C++ | Java | Python
- Sort the elements of an array : C | C++ | Java | Python
- Finding the frequency of elements in an array : C | C++ | Java | Python
- Sorting elements of an array by frequency : C | C++ | Java | Python
- Finding the Longest Palindrome in an Array : C | C++ | Java| Python
- Counting Distinct Elements in an Array : C | C++ | Java| Python
- Finding Repeating elements in an Array : C | C++ | Java | Python
- Finding Non Repeating elements in an Array : C | C++ | Java | Python
- Removing Duplicate elements from an array : C | C++ | Java | Python
- Finding Minimum scalar product of two vectors : C | C++ | Java | Python
- Finding Maximum scalar product of two vectors in an array : C | C++ | Java | Python
- Counting the number of even and odd elements in an array : C | C++ | Java | Python
- Find all Symmetric pairs in an array : C | C++ | Java | Python
- Find maximum product sub-array in a given array : C | C++ | Java | Python
- Finding Arrays are disjoint or not : C | C++ | Java | Python
- Determine Array is a subset of another array or not : C | C++ | Java | Python
- Determine can all numbers of an array be made equal : C | C++ | Java | Python
- Finding Minimum sum of absolute difference of given array : C | C++ | Java | Python
- Sort an array according to the order defined by another array : C | C++ | Java | Python
- Replace each element of the array by its rank in the array : C | C++ | Java | Python
- Finding equilibrium index of an array : C | C++ | Java| Python
- Rotation of elements of array- left and right : C | C++ | Java| Python
- Block swap algorithm for array rotation : C | C++ | Java| Python
- Juggling algorithm for array rotation : C | C++ | Java | Python
- Finding Circular rotation of an array by K positions : C | C++ | Java | Python
- Balanced Parenthesis Problem : C | C++ | Java | Python
Login/Signup to comment
#include
int main()
{
int a[50],t[50],n,mid,i,j,temp;
printf(“enter the no.of elements of array:”);
scanf(“%d”,&n);
printf(“enter the elements:”);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;ja[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(“the ascending order is :”);
for(i=0;i=0;i–)
{
printf(“%d “,a[i]);
}
return 0;
}