Sorting in Array
Sorting in Array
Sorting is the process of arranging data items in a particular order (ascending or descending). Arrays are convenient for storing many forms of data, because they allow the access of the entry using its index.
Selection Sort:
The first linear search method we will examine is called the selection sort. We perform a search through the table, starting from the first record, to locate the element with the smallest key. When this record is found , we interchange it with the first record in the table. As a result of this interchange , the smallest key is placed in the first position of the table. In the second time or iteration, we locate second smallest key examining the keys of the records starting from the second record onwards. We then interchange this record with the second record in the table. We continue this process of searching for the record with the smallest key in the remaining unsorted portion of the table and placing it in its proper place unit we have placed all the records in the proper order.
Bubble Sort
Bubble sort works by repeatedly moving the largest element to the highest index position of the array. The basic idea underlying the bubble sort is to pass through the table sequentially several times. Each pass puts the largest unsorted element in its correct place by comparing each element in the table with its successor and interchanging the two elements if they are not in proper order. The first pass puts the largest element of the array the last (nth) place, the second pass puts the second largest element in the second (n-1th) place. In general the ith iteration puts the (n-ith) element slowly “bubbles” upto its proper place, this is why the method is called bubble sort.
Insertion Sort
Insertion sort attempts to improve the number of key comparisons by moving items from the unsorted list to the correct place in the list. This sorting method divides the list into sorted part and unsorted part. It picks up one element form the front of the unsorted part and inserts it at proper place in the sorted part and repeats this action till the unsorted part is exhausted.