In this post we will be talk about Sorting Algorithm. We Try to cover all sorting algorithm only this post . in this post you get mainly 6 Types of Sorting Algorithm. here you can see which sorting algorithm we cover in this post .
- Bubble Sort
- Selection Sort
- Insertion Sort
- Quick Sort
- Merge Sort
- Heap Sort
Also Read :
Before writing Sorting Algorithm, first we see what is Sorting ?
What is Sorting ?
Sorting is a Technic used for Arrange data in a particular form such as : Ascending order, Descending Order, alphabetical Order or another ways .
With the help of sorting we analyze data in better understanding. Also we find , edit and delete data in sort time . There are many sorting Technic to sort data. but here we cover only some famous sorting Technic.
Image-Source: Pixabay.com |
Different Sorting Technic used for Different type of Operation , you can say also as per Requirement. So lets See All Sorting Algorithm.......
Sorting Algorithm | All Sorting Algorithm with Explanation
Sorting Algorithm is not a Exact Program , this is only used for checking How a sorting program works. You can write Sorting Algorithm in any language. Algorithm tell us how you write a program . if you write a Correct Algorithm , then you can write program without any logical error. and your program give correct result. In this post we Write All Sorting Algorithm In Ascending Order. if you need some another logical order , then you modify as per your Requirement.
Now we will see Sorting Algorithm.
Bubble Sort Algorithm :
In Bubble Sorting Technic we compare Started value , Suppose we have an array which name A in this array we have 5(0 - 4) Element. A[]={5,6,4,3,2}. The value Start to be compared at Index 0 and 1 (A[0] > A[1]). this is a condition , if Left Side Value is greater than Right , then these value swaped togather. This method run again and check until the array in not fully sorted.
Bubble Sort Sorting Algorithm:
In Bubble Sort Algorithm first we create a Function which takes Array and it's Size. The function name is Sort(Array , Size)
Step 1 : Start
Step 2: Set i=1 , j=0 (for loops)
Step 3: Repeat for i=1,2,3....Size-1
Step 4: Repeat for j=0,1,2.....Size-1-i
Step 5: Check if(Array[j] > Array[j+1]) if yes then Swap Array[j] with Array[j+1]
Step 6: When Array is Sort return Array
Step 7: Stop
This is a Very Easy Sorting Technic . This is the Algorithm of Bubble Sort , try to make bubble sort program himself.
Selection Sort Algorithm:
In Selection Sort , first we find a Smallest number in array after this we compare this number to Index 1 Number . This process is running again and again until the array is not fully sorted. In bubble sort the comparison start with Index 0 , but in Selection sort Comparison Start with Index 1 . Now we see what are the Sorting Algorithm of Selection sort technic .
Selection Sort Sorting Algorithm :
In selection Sort Algorithm first we create a function which return a index value of smallest number in Array. This function Takes three arguments Array , Size, and Start (Start indicate, where searching has been started) The Function Name is : MinNumber(Array , Start, Size)
Step 1: Start
Step 2: Set k=0,
Step 3: Repeat Steps 4 and 5 for K=0,1,2.....Size-2 (Where Size is Total Elements in Array )
Step 4: Call MinNumber(Array, k, Size ) and Assign Index on Location
(Location=MinNumber(Array, k, Size ))
Step 5: Interchange the value between Array[k] = Array[Location]
(Set temp= Array[k], Array[k]= Array[Location], Array[Location]= temp)
Step 6: Stop
This Sorting Technic in Better than Bubble Sort . but it's Implementation is little bit difficult.
Insertion Sort Algorithm :
This Sorting Technic is also called Shifting Sorting Technic. In this Sorting Technic we compare value by shift method . Means in bubble sort technic we comapre value and Swaped together. but in insertion sort we do not swap value , we shift the value at next position.
Insertion Sort Sorting Algorithm :
In Insertion Sort algorithm first we create a function which takes two arguments first one is Array and another is Size of Array . The Function name is InsertionSort(Array, Size)
Step 1: Start
Step 2: Set i=1, j
Step 3: Repeat Steps 4 to 6 while (i < Size)
Step 4: temp=Array[i], j= i-1
Step 5: Repeat while (j>=0) and temp < Array[j]
Array[j+1]=Array[j] and j= j-1
Step 6: Array[j+1]=temp, i=i+1
Step 7: Stop
The insertion Sort Algorithm work perfectly. if the array already sorted then the complete program is not execute. but in bubble sort program execute completely even if Array is already sorted. but Implementation of Insertion Sort is Difficult.
Heap Sort Algorithm:
This is a very difficult Sorting Technic. In this Sorting Technic first we create a Heap and then we apply some condition. Heap have a property which define the smallest value in a array is always on Left node of a Tree . It is not possible to Left node have a value greater than Root Node . if we arrange values of any Array by using this property then it's called Heapify.
Heap Sort Sorting Algorithm :
In Heap sort Algorithm we need mainly 2 function . the task of one function to create a heap and other function perform for BuildHeap . after this we can sort element in a array .
Step 1: Start
Step 2: Set i= temp =0
Step 3: Call Heap= CreateHeap(Size of Array) Function to Create a Heap
Step 4: Call BuildHeap(Heap, Array, Size) Function for Heap Building
Step 5: Intialize OldSize= Heap-> Count
Step 6: Sort Array by repeatedly Deleting the Root of Heap
Repeat for i= Size-1..........321
temp=Heap->Array[0]
Heap->Array[0]=Heap->Array[Heap->count-1]
Heap->Array[0]=temp
Heap->count--
SetValue(Heap, i)
Step 7: Assign the value Heap->count= OldSize
Step 8: Stop
The Implementation of Heap Sort is very Difficult . it is used in Tree or array to sort the data.
Quick Sort Algorithm :
Coming Soon
Quick Sort Sorting Algorithm
Coming Soon
Merge Sort Algorithm
Coming Soon
Merge Sort Sorting Algorithm
Coming Soon
Conclusion: Sorting is very important to set data in a particular form . in this post we discuss all sorting Algorithm. with the help of these Algorithm you can make Sorting Program Easily . if You find Any error on above Algorithm please tell us by comment.
********Help Your Friends By Sharing this Post *********
Tags:
sorting algorithms in c,
sorting algorithm in data structure,
bubble sort algorithm in c
bubble sort algorithm and program in c
the bubble sort algorithm
write a bubble sort algorithm
Insertion Sort Algorithm
Selection sort Algorithm
Heap Sort Algorithm
0 Comments
Hey , Comment Your Query or Suggestion