Skip to main content

Posts

Showing posts with the label sorting

Bubble Sort

Bubble Sorting Among variaous other sorting algorithm, bubble sort algorithm is one of the popular and frequently used algorithm to sort elements either in ascending or descending order. Bubble sort algorithm starts by comparing the first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, you mustn't swap the element. Then, again second and third elements are compared and swapped if it is necessary and this process go on until last and second last element is compared and swapped. This completes the first step of bubble sort. If there are  n  elements to be sorted then, the process mentioned above should be repeated  n-1  times to get required result. But, for better performance, in second step, last and second last elements are not compared becuase, the prop...

Insertion Sorting-Some Extra Stuff

Insertion Sorting Intro:- Insertion sort is one of the many sorting algorithms which implements sorting in a very elegant way. It is much less efficient on large lists than more advanced algorithms such as  quicksort , heapsort, or  merge sort .Its algo is easy to understand.If the first few objects are already sorted, an unsorted object can be inserted in the sorted set in proper place. This is called insertion sort. An algorithm consider the elements one at a time, inserting each in its suitable place among those already considered (keeping them sorted). Insertion sort is an example of an incremental  algorithm; it builds the sorted sequence one number at a time. This is perhaps the simplest example of the incremental insertion technique, where we build up a complicated structure on n items by first building it on n − 1 items and then making the necessary changes to fix things in adding the last item. The given sequences are typically stored in arrays....

Selection Sort

Selection Sorting Selection sort algorithm starts by compairing first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, leave the elements as it is. Then, again first element and third element are compared and swapped if necessary. This process goes on until first and last element of an array is compared. This completes the first step of selection sort. If there are  n  elements to be sorted then, the process mentioned above should be repeated  n-1  times to get required result. But, for better performance, in second step, comparison starts from second element because after first step, the required number is automatically placed at the first (i.e, In case of sorting in ascending order, smallest element will be at first and in case of sorting in descending...

Merge-Sort

Merge Sorting Merge sorting also works on the principle of "divide and conquer".The algorithm of merge sort is quite different from that of quick-sort but its best case performance is also O(nlog(n)).The step by step process is- Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted). Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list. How it is actually implemented is described here.... Firstly divide the given array of n elements into n individual numbers. Then compare the adjacent elements and merge them in the sorted order. Then compare the corresponding elements of the resulting lists and arrange them in the sorted order. And finally the sorted list will be obtained. The following link will help you in understanding it. Want to see the example code..?? CLICK HERE Complexity Analysis of Merge Sort Worst Case Ti...

Quick-Sort

Quick Sort, as the name suggests, sorts any list very quickly. Quick sort is not stable search, but it is very fast and requires very less aditional space. It is based on the rule of Divide and Conquer (also called partition-exchange sort ). This algorithm divides the list into three main parts : Elements less than the Pivot element Pivot element Elements greater than the pivot element As the image suggests, a pivot is selected at first which is 4 in our case.It then compares the other members with the pivot and divides the list into three main parts as discussed above. The next step is to call this function recursively.It will return one number if only one member is left in any of the three parts. Complexity Analysis of Quick Sort Worst Case Time Complexity : O(n 2 ) Best Case Time Complexity : O(n log n) Average Time Complexity : O(n log n) Space Complexity : O(n log n) Space required by quick sort is very less, only O(n log n) additional space is re...