This chapter examines various sorting algorithms, including bubble sort, insertion sort, selection sort, quick sort, merge sort, heap sort, radix sort, and shell sort. It covers their algorithms, complexities, and comparisons.
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high - 1
if arr[j] < pivot
i = i + 1
swap(arr[i], arr[j])
swap(arr[i + 1], arr[high])
return i + 1
Initially: 3025701948282144120
Choose pivot 120, partition around it.
Pass 1: 3025701948282144120
Apply quickSort to subarray 30, 25, 70, 19, 48, 28, 21, 44.Choose pivot 44, partition around it.
Pass 2: 3025192128447048
Apply quickSort to subarray 30, 25, 19, 21, 28.Choose pivot 28, partition around it.
Pass 3: 3025192128
Apply quickSort to subarray 30, 25, 19, 21.Choose pivot 21, partition around it.
Pass 4: 19212530
Apply quickSort to subarray 25, 30.Choose pivot 30, partition around it.
Pass 5: 2530
Apply quickSort to subarray 70, 48.Choose pivot 48, partition around it.
Explain selection sort. Sort data sequence 409020-103056010080 using selection sort method.
Answer:
Selection Sort is an unstable, in-place, comparison-based sorting algorithm that repeatedly selects the smallest (or largest) element from the unsorted portion and places it in its correct position. This process continues for n-1 passes to sort an array of n elements. In each iteration of selection sort, the minimum element (considering ascending order) from
the unsorted subarray is picked and moved to the sorted subarray.
Define internal and external sorting. Write an algorithm for quick sort and trace your algorithm for a given sequence of data. 5, 43, 99, 20, 45, 7, 6, 63, 92, 4.
Solution:
Internal sorting is a type of sorting that takes place in the main memory (RAM) of a computer. It is used when the data to be sorted fits entirely within the available memory. Examples of internal sorting algorithms include quicksort, mergesort, and heapsort.
External sorting is a type of sorting that is used when the data to be sorted does not fit entirely in the main memory. It involves using external storage devices, such as hard drives or SSDs, to store and sort the data. External sorting algorithms are designed to minimize the number of disk accesses and efficiently handle large datasets. Examples of external sorting algorithms include external mergesort and polyphase mergesort.
partition(arr, low, high)
pivot = arr[high]
i = low - 1
for j = low to high - 1
if arr[j] < pivot
i = i + 1
swap(arr[i], arr[j])
swap(arr[i + 1], arr[high])
return i + 1
Initially: 5439920457663924
Choose pivot 4, partition around it.
Pass 1: 4 43 99 20 45 7 6 63 92 5
Apply quickSort to subarray 43, 99, 20, 45, 7, 6, 63, 92, 5.Choose pivot 5, partition around it.
Pass 2: 4599204576639243
Apply quickSort to subarray 99, 20, 45, 7, 6, 63, 92, 43.Choose pivot 43, partition around it.
Pass 3: 4520764345926399
Apply quickSort to subarray 20, 7, 6.Choose pivot 6, partition around it.
Pass 4: 4567204345926399
Apply quickSort to subarray 7, 20.Choose pivot 20, partition around it.
Pass 5: 4567204345926399
Apply quickSort to subarray 45, 92, 63, 99.Choose pivot 99, partition around it.
Pass 6: 4567204345926399
Apply quickSort to subarray 45, 92, 63.Choose pivot 63, partition around it.
Pass 7: 4567204345639299
Apply quickSort to subarray 45, 92.Choose pivot 92, partition around it.