Sorting of Array in C

Sorting of Array in C Language

In this page we will look into a coding question where we will learn about sorting of array in C programming language. There are many sorting techniques to sort the array like quick sort, merge sort, bubble sort, insertion sort and selection sort. There might be different approach to solve this question, one you will find here. If your approach is bit different post it onto the comment section.

Sorting of Array In C Programming

Sorting is the process of arranging elements in a list or array in a specific order, typically in ascending or descending order. Sorting is a fundamental problem in computer science and has many applications in areas such as searching, data compression, and data analysis.

There are many sorting algorithms that can be used to sort an array. Some of the most popular algorithms include:

1. Bubble Sort: This is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

2. Selection Sort: This algorithm sorts an array by repeatedly finding the minimum element from the unsorted part of the array and putting it at the beginning.

3. Insertion Sort: This algorithm builds the final sorted array one item at a time, by inserting each item in the correct position in the array.

4. Merge Sort: This algorithm divides the array into two halves, sorts each half separately, and then merges the two halves together to form a sorted array.

5. Quick Sort: This algorithm picks an element as a pivot and partitions the array around the pivot, such that elements smaller than the pivot are on one side and elements larger than the pivot are on the other side. It then recursively sorts the two sub-arrays.

Algorithm :

• Take the size of the array from the user.
• Declare an array of given input size.
• Take the input of all elements of the array.
• Now run a for loop from 0 to size-1.
• And for every element check it from all the next elements to it. If the element is greater than swap that number.
• In this way the array will get sorted in ascending order.

Program for sorting the elements of c in ascending order Using Selection Sort

Run

```#include<stdio.h>
int main()
{

int a[6]= {12,5,10,9,7,6};
int temp;
int i, j;

printf("Before Sorting ");

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

for(i=0; i<6; i++)
{
for(j=i+1; j<6; j++) { if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("\nAfter Sorting ");
for(i=0; i<6; i++)
{
printf("%d ",a[i]);
}
return 0;
}
```

Output:

```Before Sorting 12 5 10 9 7 6
After Sorting 5 6 7 9 10 12```

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Traversals

• Traversal in Trees
• Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
• Tree Traversals: Depth First Search (DFS) : C | C++ | Java
• Construct a Binary Tree from Postorder and Inorder

AVL Trees

• AVL Trees
• AVL Trees: Introduction
• AVL Tree Insertion : C | C++ | Java
• AVL Tree Deletion : C | C++ | Java
• Insertion in a Binary Tree (Level Order) – C | C++ | Java
• Searching in Binary Tree – C | C++ | Java
• Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

• Depth First Traversals – C | C++ | Java
• Level Order Traversal – C | C++Java
• Construct Tree from given Inorder and Preorder traversals – C | C++Java
• Construct Tree from given Postorder and Inorder traversals – C | C++Java
• Construct Tree from given Postorder and Preorder traversal – C | C++Java
• Find size of the Binary tree – C | C++Java
• Find the height of binary tree – C | C++Java
• Find maximum in binary tree – C | C++Java
• Check whether two tree are identical- CC++Java
• Spiral Order traversal of Tree- CC++Java
• Level Order Traversal Line by Line – C | C++Java
• Hand shaking lemma and some Impotant Tree Properties.
• Check If binary tree if Foldable or not.- CC++Java
• check whether tree is Symmetric – C| C++Java.
• Check for Children-Sum in Binary Tree- C|C++Java
• Sum of all nodes in Binary Tree- CC++ | Java
• Lowest Common Ancestor in Binary Tree- CC++ | Java

Traversals

• Traversal in Trees
• Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
• Tree Traversals: Depth First Search (DFS) : C | C++ | Java
• Construct a Binary Tree from Postorder and Inorder

AVL Trees

• AVL Trees
• AVL Trees: Introduction
• AVL Tree Insertion :  C | C++ | Java
• AVL Tree Deletion : C | C++ | Java
• Insertion in a Binary Tree (Level Order) – C | C++ | Java
• Searching in Binary Tree – C | C++ | Java
• Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

• Depth First Traversals – C | C++ | Java
• Level Order Traversal – C | C++Java
• Construct Tree from given Inorder and Preorder traversals – C | C++Java
• Construct Tree from given Postorder and Inorder traversals – C | C++Java
• Construct Tree from given Postorder and Preorder traversal – C | C++Java
• Find size of the Binary tree – C | C++Java
• Find the height of binary tree – C | C++Java
• Find maximum in binary tree – C | C++Java
• Check whether two tree are identical- CC++Java
• Spiral Order traversal of Tree- CC++Java
• Level Order Traversal LIne by Line – C | C++Java
• Hand shaking lemma and some Impotant Tree Properties.
• Check If binary tree if Foldable or not.- CC++Java
• check whether tree is Symmetric  C| C++Java.
• Check for Children-Sum in Binary Tree- C|C++Java
• Sum of all nodes in Binary Tree- CC++ | Java
• Lowest Common Ancestor in Binary Tree. CC++ | Java