Saturday, July 23, 2022

Data structures and algorithms cheat sheet - Sorting algorithms comparison

Data structures and algorithms cheat sheet, sorting algorithms quick reference, comparison of sorting algorithms on auxiliary space used, sorting algorithms cheat sheet, stable vs in-place vs comparison sorting algorithms quick reference

 

Sorting algorithms - A comparison

Algorithm

Time complexity

Space complexity

In-place

Stable

Comparison

No. of comparisons

Best

Average

Worst

Total

Auxiliary

Min

Max

Bubble sort

O(n)

O(n2)

O(n2)

O(n)

O(1)

Yes

Yes

Yes

n(n-1)/2

n(n-1)/2

Selection sort

O(n2)

O(n2)

O(n2)

O(n)

O(1)

Yes

No

Yes

n(n-1)/2

n(n-1)/2

Insertion sort

O(n)

O(n2)

O(n2)

O(n)

O(1)

Yes

Yes

Yes

n-1

n(n-1)/2

Merge sort

O(n log n)

O(n log n)

O(n log n)

O(n)

O(n)

No

Yes

Yes

n*log 2 n

n*log 2 n

Quick sort

O(n log n)

O(n log n)

O(n2)

O(n)

O(1)

Yes

No

Yes

n*log2n 

n(n-1)/2

Heap sort

O(n log n)

O(n log n)

O(n log n)

O(n)

O(1)

Yes

No

Yes

n*log2n

n*log2n

Bucket sort

O(n+k)

O(n+k)

O(n2)

O(n+k)

 

No

Yes

No

Depends on the algorithm used to sort the elements in each bucket

Radix sort

O(nk)

O(nk)

O(nk)

O(n+k)

 

Yes

Yes

No

NA

Shell sort

O(n log n)

O(n log2n)

O(n log2n)

O(n)

O(1)

Yes

No

Yes

n*log2n

 

 

Notations used

  • n refers to number of elements to be sorted.
  • k in Bucket sort refers to number of buckets, in Radix sort refers to the maximum length of the input elements. 

 

Auxiliary Space

is the temporary space allocated by your algorithm to solve the problem, with respect to input size.

 

Space Complexity

is the total space used by your algorithm to solve the problem, with respect to input size. Note that the Space Complexity includes input size.

 

In-place sorting algorithm

A sorting algorithm is in-place if it requires only O(1) extra space to sort the array.

 

Stable sorting algorithm

We say that a sorting algorithm is stable if, when two records have the same key, they stay in their original order. A sort is stable if two elements with the same value maintain their same relative order before and after the sort is performed.

 

Comparison sorting algorithm

Any sort algorithm using comparisons between keys (elements), and nothing else about the keys, to arrange items in a predetermined order (ascending or descending). The predetermined sorted order is determined by a sequence of comparisons between pairs of elements.


********************

Related links

Go to Data structures home page

 

Sorting algorithm comparison on time and space complexity

Sorting algorithm comparison on stability, and auxiliary space usage

Data structures and algorithms cheat sheet

Sorting algorithms quick reference 

Comparison of sorting algorithms in DSA on space utilized

Thursday, July 21, 2022

Machine Learning MCQ - Cost function of logistic regression is convex

Multiple choices questions in Machine learning. Interview questions on machine learning, quiz questions for data scientist answers explained, what is the cost function of logistic regression? Why the cost function used in linear regression cannot be used in logistic regression? Why the cost function of logistic regression is convex?

Machine Learning MCQ - Cost function of logistic regression is convex

< Previous                      

Next >

 

1. Which of the following statements about logistic regression are correct?

a) Logistic regression uses the squared error as the loss function

b) Logistic regression assumes that each class’s points are generated from a Gaussian distribution

c) The cost function of logistic regression is concave

d) The cost function of logistic regression is convex

Answer: (d) The cost function of logistic regression is convex


Gradient descent will converge into global minimum only if the cost function is convex in the case of logistic regression.

 

Can we use the same cost function used in linear regression for logistic regression?

If we use the cost function of linear regression (Mean Squared Error) in logistic regression, we end up with a non-convex function with many local minimums. In this case, it is very difficult to find the global minimum. This strange outcome is due to the fact that in logistic regression we have the sigmoid function around, which is non-linear (i.e. not a line).

   

< Previous                      

Next >

 

************************

Related links:

Why the cost function of logistic regression is convex?

Why can't we use the cost function of linear regression in the case of logistic regression?

Can we use the same cost function of linear regression in logistic regression?

What will happen if you use linear regression's cost function in logistic regression?

Machine learning solved mcq, machine learning solved mcq

Featured Content

Multiple choice questions in Natural Language Processing Home

MCQ in Natural Language Processing, Quiz questions with answers in NLP, Top interview questions in NLP with answers Multiple Choice Que...

All time most popular contents

data recovery