Sorting - · PDF fileSorting •Various sorting techniques are: •Insertion sort...

23

Transcript of Sorting - · PDF fileSorting •Various sorting techniques are: •Insertion sort...

Sorting• Sorting means arranging the elements of an array so that they are placed

in some relevant order which may be either ascending or descending.

• That is, if A is an array, then the elements of A are arranged in a sortedorder (ascending order) in such a way that A[0] < A[1] < A[2] < … < A[N].

• The practical considerations for different sorting techniques would be:• Number of sort key comparisons that will be performed.

• Number of times the records in the list will be moved.

• Best, average and worst case performance.

• Stability of the sorting algorithm where stability means that equivalent elements orrecords retain their relative positions even after sorting is done.

Sorting• Various sorting techniques are:

• Insertion sort

• Selection sort

• Bubble sort

Insertion Sort• In insertion sort, the sorted array is built one at a time.

• The main idea behind insertion sort is that it inserts each item into itsproper place in the final list.

• To save memory, most implementation of the insertion sort algorithmwork by moving the current data element past the already sorted valuesand repeatedly interchanging it with the preceding value until it is in itscorrect place.

Insertion Sort• Technique:

• The array of values to be sorted is divided into two sets. One that stores sorted valuesand another that contains unsorted values.

• The sorting algorithm will proceed until there are elements in the unsorted set.

• Suppose there are n elements in the array. Initially, the element with index 0(assuming LB = 0) is in the sorted set. Rest of the elements are in the unsorted set.

• The first element of the unsorted partition has array index 1 (if LB = 0)

• During each iteration of the algorithm, the first element in the unsorted set is pickedup and inserted into the correct position in the sorted set.

Algorithm of Insertion Sort1. BEGIN

2. Get input A[n]

3. Repeat thru step 7 for i = 0 to n

4. temp A[i]

5. j i

6. Repeat while temp ≤ A[j] and j ≥ 07. A[j + 1] A[j]

8. j j – 1

9. A[j + 1] = temp

10. END

Insertion Sort

Selection Sort• The algorithm divides the input list into two parts:

• the sublist of items already sorted, which is built up from left to right at the front(left) of the list,

• and the sublist of items remaining to be sorted that occupy the rest of the list.

• Initially, the sorted sublist is empty and the unsorted sublist is the entireinput list.

• The algorithm proceeds by finding the smallest (or largest, depending onsorting order) element in the unsorted sublist, exchanging it with theleftmost unsorted element (putting it in sorted order), and moving thesublist boundaries one element to the right.

Selection Sort• Technique:

• Consider an array ARR with N elements. The selection sort takes N-1 passes to sortthe entire array and works as follows. First find the smallest value in the array andplace it in the first position. Then find the second smallest value in the array andplace it in the second position. Repeat this procedure until the entire array is sorted.Therefore,

• In Pass 1, find the position POS of the smallest value in the array and then swapARR[POS] and ARR[0]. Thus, ARR[0] is sorted.

• In Pass 2, find the position POS of the smallest value in sub-array of N-1 elements.Swap ARR[POS] with ARR[1]. Now, A[0] and A[1] is sorted

• In Pass 3, find the position POS of the smallest value in sub-array of N-2 elements.Swap ARR[POS] with ARR[2]. Now, ARR[0], ARR[1] and ARR[2] is sorted.

• In Pass N-1, find the position POS of the smaller of the elements ARR[N-2] andARR[N-1]. Swap ARR[POS] and ARR[N-2] so that ARR[0], ARR[1], … , ARR[N-1] issorted.

Algorithm of Selection Sort1. BEGIN

2. Get input A[n]

3. Repeat thru step 4 for i = 1 to n-1

4. Repeat for j = i + 1, … , n5. If A[j] < A[i]

temp = A[i]

A[i] = A[min_index]

A[min_index] = temp

END

6. END

Selection Sort

Bubble Sort• Bubble sort is a very simple method that sorts the array elements by

repeatedly moving the largest element to the highest index position of

the array (in case of arranging elements in ascending order).

• In bubble sorting, consecutive adjacent pairs of elements in the array

are compared with each other.

• If the element at lower index is greater than the element at the higher

index, the two elements are interchanged so that the smaller element is

placed before the bigger one.

• This process is continued till the list of unsorted elements

exhaust.

Bubble Sort• Technique:

• In Pass 1, A[0] and A[1] are compared, then A[1] is compared with A[2], A[2] iscompared with A[3] and so on. Finally, A[N-2] is compared with A[N-1]. Pass 1involves N-1 comparisons and places the biggest element at the highest index of thearray.

• In Pass 2, A[0] and A[1] are compared. then A[1] is compared with A[2], A[2] iscompared with A[3] and so on. Finally, A[N-3] is compared with A[N-2]. Pass 2involves N-2 comparisons and places the second biggest element at the secondhighest index of the array.

• In Pass 3, A[0] and A[1] are compared. then A[1] is compared with A[2], A[2] iscompared with A[3] and so on. Finally, A[N-4] is compared with A[N-3]. Pass 3involves N-3 comparisons and places the third biggest element at the third highestindex of the array.

• In Pass n-1, A[0] and A[1] are compared so that A[0] < A[1]. After this step, all theelements of the array are arranged in ascending order.

Algorithm of Bubble Sort1. BEGIN

2. Get input A[n]

3. Repeat thru step 5 for i = 0 to n

4. exchgs 0

5. Repeat thru step 6 for j = 0 , … , n – i - 1

6. If A[j] > A[j + 1]BEGIN

temp = A[j]

A[j] = A[j + 1]

A[j + 1] = temp

exchgs exchgs + 1

END

7. If exchgs = 0BEGIN

EXIT

END

8. END

Bubble Sort

Searching• The basic characteristics of any searching algorithm is that

• searching should be efficient,

• it should have less number of computations involved into it,

• as well as the space occupied by such a technique should be less.

• Various searching techniques are:• Linear search

• Binary search

Linear Search / Sequential Search• It is a method for finding a particular value in a list that checks each

element in sequence until the desired element is found or the list isexhausted.

• The list need not be ordered.

• Linear search is usually very simple to implement, and is practical whenthe list has only a few elements, or when performing a single search inan unordered list.

• If the search element is not in the array, then it scans the entire array, sotakes time as per the number of elements in the array i.e. n.

Algorithm of Linear Search1. BEGIN

2. Get input A[n] and X (element to be searched)

3. i 0

4. Repeat thru step 6 for i < n

5. If A[i] == X

BEGIN

Print “Match found at array index i”

ExitEND

6. i i + 1

7. Print “Match not found”

8. END

Linear Search / Sequential Search

Binary Search• A binary search or half-interval search algorithm finds the position of a

specified input value (the search "key") within an array sorted by key value.

• For binary search, the array should be arranged in ascending or descendingorder.

• In each step, the algorithm compares the search key value with the key value ofthe middle element of the array.

• If the keys match, then a matching element has been found and its index, orposition, is returned.

• Otherwise, if the search key is less than the middle element's key, then thealgorithm repeats its action on the sub-array to the left of the middle elementor, if the search key is greater, on the sub-array to the right.

• If the remaining array to be searched is empty, then the key cannot be found inthe array and a special "not found" indication is returned.

• A binary search halves the number of items to check with each iteration, solocating an item (or determining its absence) takes logarithmic time.

• It requires the array to be in sorted order.

Algorithm of Binary Search1. BEGIN

2. Get input A[n] and X (element to be searched)

3. low 0

4. high n - 1

5. Repeat thru step while low ≤ high

6. middle (low + high) / 2

7. if X < A[middle]BEGIN

highmiddle - 1

ENDelse

BEGINif X > A[middle]BEGIN

lowmiddle + 1ENDelseBEGIN

Print “Successful search”

ENDEND

8. END

Binary Search

ANY QUESTIONS???