Posts

Showing posts with the label Sorting techniques

Selection sort

Image
Selection sorting technique in python and C Selection sort  It is one of the sorting techniques. In this technique, we repeatedly sort the array by finding the minimum element in the array and swapping the minimum element with the element in the first index, the second minimum element with the element in the second index, and so on. By repeating the procedure we will be having two sub arrays, a sorted array, and an unsorted array. At the end, finishing the above procedure till the last index we'll be having a single sub array that is the sorted array. Explanation : Selection sort Look at the above example, where we have the array [5, 3, 4, 1, 6, 2] to be sorted. At First, we have to find the first minimal element in the array, the minimum element is 1. Now swap it with the element at index 0, which is 5. After swapping 1 and 5, the array will be [1, 3, 4, 5, 6, 2]. Then swap the second smallest element (2)  with the element at index 1 ( 3), the array will be [1, 2, 4, 5, 6, 3]. Swa

Insertion sort in C, python

Image
Insertion sort                      Insertion sort is a simple sorting technique. It is almost similar to the way we sort the playing cards. The whole array is split into two parts namely the sorted and unsorted. The elements from the unsorted array are placed at the correct position in the sorted array. The process continues until the entire unsorted array gets sorted out. Procedure : Explanation :           Now let us consider an array [7,1,4,2]. We have to start from the element at index 1 and compare it with all the previous elements and arrange the elements in the sorted format. Continue the same process until we reach the last element, so that the entire array gets sorted. For example,  for the array [7,1,4,2], starting with the element 1 at index 1, compare it with previous element 7 ,as 1 is less than 7 swap the elements 1 and 7. The array becomes [1,7,4,2]. Now move onto the next index position. Compare the element at index 2 with the the previous elements and arrange them acc