Comparing Sorting Algorithms

Level 1

For the following list, which two sorting algorithms have the same running time (ignoring constant factors)?

A = [ 4 , 2 , 0 , 9 , 8 , 1 ] A = [4,2,0,9,8,1]

Mergesort and Bubble Sort Insertion Sort and Mergesort None of the Above Bubble Sort and Insertion Sort

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Karleigh Moore
Mar 24, 2016

This list is not already ordered. Because of this, Insertion Sort takes O ( n 2 ) O(n^2) time, Bubble Sort takes O ( n 2 ) O(n^2) time, and Mergesort takes O ( n lg n ) O(n \lg n) time.

Which is key in pseudo code

Adeeb Hassan - 2 years, 5 months ago
Veron Ibishi
Jun 14, 2021

Bubble Sort Pseudo Code do { swapped false ; for { int i = 1 to IndexofLastUnsortedElemtent -1 if LeftElement > RightElement swap(leftelement,rightelement) swappped true; } }while(swapped); Because the Sorting uses two loops to sort the input the BubbleSort Algorithm is equal to that of 2 loops which is N * N = N^2 . Insertion Sort Pseudo Code ( mark first elemtn as sorted ; 'extract' the element X; for j = lastSortedIndex down to 0 ; if current element j>x; move sorted element to the right by 1; break loop and insert the X right there . While Insertion Sort is quite better if it mostly sorted this array isn't so we take the worst possible case which is N^2;

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...