Apply bubble sort, insertion sort, counting sort, heap sort, quick sort, and merge sort on the followingarray: 15,25,20,22,30,10,1,9,15,50 and write pseudocode of each and analyze their time complexity
Question
Apply bubble sort, insertion sort, counting sort, heap sort, quick sort, and merge sort on the followingarray: 15,25,20,22,30,10,1,9,15,50 and write pseudocode of each and analyze their time complexity
Solution
Sure, let's start with Bubble Sort:
Bubble Sort:
function bubbleSort(arr)
n = length(arr)
for i from 0 to n-1
for j from 0 to n-i-1
if arr[j] > arr[j+1]
swap arr[j] and arr[j+1]
return arr
Time complexity: O(n^2)
Insertion Sort:
function insertionSort(arr)
n = length(arr)
for i from 1 to n
key = arr[i]
j = i-1
while j >= 0 and arr[j] > key
arr[j+1] = arr[j]
j = j-1
arr[j+1] = key
return arr
Time complexity: O(n^2)
Counting Sort:
function countingSort(arr)
max = maximum value in arr
count = array of zeros of size max+1
for i from 0 to length(arr)
increment count[arr[i]]
i = 0
for j from 0 to max
for k from 0 to count[j]
arr[i] = j
i = i+1
return arr
Time complexity: O(n+k) where k is the range of the non-negative key values.
Heap Sort:
function heapSort(arr)
n = length(arr)
for i from n/2-1 to 0
heapify(arr, n, i)
for i from n-1 to 0
swap arr[0] and arr[i]
heapify(arr, i, 0)
return arr
Time complexity: O(n log n)
Quick Sort:
function quickSort(arr, low, high)
if low < high
pi = partition(arr, low, high)
quickSort(arr, low, pi - 1)
quickSort(arr, pi + 1, high)
return arr
Time complexity: O(n log n) in the best case, O(n^2) in the worst case.
Merge Sort:
function mergeSort(arr)
if length(arr) > 1
mid = length(arr) / 2
left = arr elements from 0 to mid
right = arr elements from mid to end
mergeSort(left)
mergeSort(right)
merge(left, right, arr)
return arr
Time complexity: O(n log n)
Please note that these are pseudocodes and may not run directly in a programming language. You need to implement them according to the syntax of your preferred language.
Similar Questions
Which of the following sorting algorithm is the best in terms of time complexity?a) Bubble sort b) Heap sort c) Selection sort d) Insertion sort
Write an algorithm/pseudocode to sort elements using Heap sort technique?
The complexity of Bubble sort is ans. O(n3) O(n log n) O(n) O(log n)
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. The problem with bubble sort is its worst case scenario. When the smallest element is in the last position, then it takes more time to sort in ascending order, but takes less time to sort in descending order.An array is called beautiful if all the elements of the array are in either ascending or descending order. Given an array of numbers, find the minimum swap operations required to make the array beautiful.
What is the time complexity of the Bubble Sort algorithm used in this program? (Bonus the Answer is C)*1 pointA. O(n)B. O(n log n)C. O(n^2)D. O(1)What is the value of 'n' after sorting the array?*1 pointA. 10B. 9C. 11D. sizeof(arr)If the array had all elements equal, what would be the best-case time complexity of the Bubble Sort algorithm?(Bonus Answer is A)*1 pointA. O(n)B. O(n log n)C. O(n^2)D. O(1)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.