Data Structure-Sort
Data Structure-Sort
Kaustuv Bhattacharjee
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Sorting-Introduction
Sorting-Types
• Two types :
– Internal sorting which deals with sorting the data stored in the
computer’s memory
– External sorting which deals with sorting the data stored in files.
External sorting is applied when there is voluminous data that
cannot be stored in the memory.
Sorting-Methods
• Bubble Sort
• Insertion Sort
• Selection Sort
• Quick Sort
• Merge Sort
Bubble Sort
• After end of the first pass, the largest element is placed at the highest index of the array. All the other
elements are still unsorted.
• This is continued in next few passes until all the elements are sorted in ascending order.
Kaustuv Bhattacharjee, UEM Kolkata
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
BUBBLE_SORT(A, N)
Step 4: EXIT
Insertion Sort
• This algorithm inserts each item into its proper place in the
final list.
Insertion Sort-Technique
Insertion Sort-Explanation
Pass 10 9 18 36 39 45 54 63 72 81 108
Unsorted Sorted
• Explanation:
• Initially, A[0] is the only element in the sorted set. In Pass 1, A[1] will
be placed either before or after A[0], so that the array A is sorted. In Pass 2,
A[2] will be placed either before A[0], in between A[0] and A[1], or after
A[1]. In Pass 3, A[3] will be placed in its proper place. In Pass N–1, A[N–1]
will be placed in its proper place to keep the array sorted.
Kaustuv Bhattacharjee, UEM Kolkata
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Insertion Sort-Algorithm
INSERTION-SORT (ARR, N)
Step 1: Repeat Steps 2 to 5 for K = 1 to N – 1
Step 2: SET TEMP = ARR[K]
Step 3: SET J = K - 1
Step 4: Repeat while TEMP <= ARR[J]
SET ARR[J + 1] = ARR[J]
SET J = J - 1
[END OF INNER LOOP]
Step 5: SET ARR[J + 1] = TEMP
[END OF LOOP]
Step 6: EXIT
39 9 45 63 18 81108 54 72 36
Kaustuv Bhattacharjee, UEM Kolkata
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Selection Sort
Technique:
First find the smallest value in the array and place it in the first position. Then,
find the second smallest value in the array and place 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
swap ARR[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, ARR[0] and ARR[1] is sorted.
In Pass N–1, find the position POS of the smaller of the elements ARR[N–2]
and ARR[N–1]. Swap ARR[POS] and ARR[N–2] so that ARR[0], ARR[1], ...,
ARR[N–1] is sorted.
Selection Sort-Example
Selection Sort-Algorithm
SELECTION SORT(ARR, N)
Step 1: Repeat Steps 2 and 3 for K = 0 to N-1
Step 2: CALL SMALLEST(ARR, K, N, POS)
Step 3: SWAP A[K] with ARR[POS]
[END OF LOOP]
Step 4: EXIT
In the algorithm, during the Kth pass, find the position POS of
the smallest elements from ARR[K], ARR[K+1], ..., ARR[N].
Merge Sort
Merge Sort-Technique
Merge Sort-Example
• Compare ARR[I] and ARR[J], the smaller of the two is placed in TEMP at the
location specified by INDEX and subsequently the value I or J is incremented.
• When I is greater than MID, copy the remaining elements of the right sub-array in TEMP.
Merge Sort-Algorithm
MERGE (ARR, BEG, MID, END)
Step 1: [INITIALIZE] SET I = BEG, J = MID + 1, INDEX = 0
Step 2: Repeat while (I <= MID) AND (J<=END)
IF ARR[I] < ARR[J]
SET TEMP[INDEX] = ARR[I]
SET I = I + 1
ELSE
SET TEMP[INDEX] = ARR[J]
SET J = J + 1
[END OF IF]
SET INDEX = INDEX + 1
[END OF LOOP]
Step 3: [Copy the remaining elements of right sub-array, if any]
IF I > MID
Repeat while J <= END
SET TEMP[INDEX] = ARR[J]
SET INDEX = INDEX + 1, SET J = J + 1
[END OF LOOP]
[Copy the remaining elements of left sub-array, if any]
ELSE
Repeat while I <= MID
SET TEMP[INDEX] = ARR[I]
SET INDEX = INDEX + 1, SET I = I + 1
[END OF LOOP]
[END OF IF]
Step 4: [Copy the contents of TEMP back to ARR] SET K= INDEX
Step 5: Repeat while K < INDEX
SET ARR[K] = TEMP[K]
SET K = K + 1
[END OF LOOP]
Step 6: END Kaustuv Bhattacharjee, UEM Kolkata
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Quick Sort
Quick Sort-Technique
Quick Sort-Technique
• 3. Start from the element pointed by left and scan the array from left to right,
comparing each element on the way with the element pointed by loc.
That is, a[loc] should be greater than a[left].
(a) If that is the case, then simply continue comparing until left becomes
equal to loc. Once left = loc, it means the pivot has been placed in its correct
position.
(b) However, if at any point, we have a[loc] < a[left], then interchange the
two values and jump to Step 2.
(c) Set loc = left.
Quick Sort-Example
Quick Sort-Algorithm
PARTITION (ARR, BEG, END, LOC)
Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG = 0
Step 2: Repeat Steps 3 to 6 while FLAG = 0
Step 3: Repeat while ARR[LOC] <= ARR[RIGHT] AND LOC != RIGHT
SET RIGHT = RIGHT - 1
[END OF LOOP]
Step 4: IF LOC = RIGHT
SET FLAG = 1
ELSE IF ARR[LOC] > ARR[RIGHT]
SWAP ARR[LOC] with ARR[RIGHT]
SET LOC = RIGHT
[END OF IF]
Step 5: IF FLAG = 0
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT + 1
[END OF LOOP]
Step 6: IF LOC = LEFT
SET FLAG = 1
ELSE IF ARR[LOC] < ARR[LEFT]
SWAP ARR[LOC] with ARR[LEFT]
SET LOC = LEFT
[END OF IF]
[END OF IF]
Step 7: [END OF LOOP]
Step 8: END
Kaustuv Bhattacharjee, UEM Kolkata
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Sort-Assignments
Thank You