4-Sorting algorithms (selection - insertion - bubble - Quick ) Flashcards

1
Q

Goal of sorting ?

A

The goal of sorting is to rearrange the elements of S ( a sequence {s1,s2,s3,…,sn} of n>=0 elements drawn from a universe U ) and produce another sequence S’ such that elements of S appear in order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does it mean to say “in order”?

A

Given a relation, eg. < :

  • It has to be a total order (simple order)
  • For all pairs of elements (i,j), exact one of the following is true: i i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a stable sort?

A

When it preserves the relative order of the items with duplicated keys on the list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Advantages of a stable sort:

A
  • more efficient in practice
  • in general, easier to be parallelised
  • allow multi-key sort to be easily implemented by combining multiple runs the sorting algorithm.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does selection sort work ?

A

selection sort orders a list of elements by finding the

index of the maximum element (selecting the element) and putting it in its place in the list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Characteristics of selection sort ?

A
  • It is a simple algorithm
  • it is inefficient for large values of n, where n is the size of the list.
  • Can be easily implemented on linked lists
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write down the Selection sort algorithm.

A
void selectionSort(int list[]) {
 \_\_\_\_int last = list.length;
 \_\_\_\_int maxPos;
 \_\_\_\_while (last > 0) {
 \_\_\_\_\_\_\_\_maxPos = findMax(list,last+1);
 \_\_\_\_\_\_\_\_swap(list,maxPos,last);
 \_\_\_\_\_\_\_\_last--;
 \_\_\_\_}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

WORST CASE selection sort

A

theta(n^2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain Insertion Sort :

A

The idea here is that we get an element and insert it in the position it belongs.

Used frequently when we want to generate another list with the elements
sorted

It can be also done in place, that is, without requiring an extra list

After each outer loop interaction one element in the list has been placed in his final location

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What performance do elementary algorithms have ?

A

Theta(n^2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Examples of elementary algorithms:

A

Insertion and selection sort

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write an insertion sort algorithm :

A
void insertionSort(int list[]) {
 \_\_\_\_int pos = list.length-1;
 \_\_\_\_while (pos > 0) {
\_\_\_\_\_\_\_\_ indexNew = pos – 1;
 \_\_\_\_\_\_\_\_valueNew = list[indexNew];
\_\_\_\_\_\_\_\_\_while ((indexNew < list.length-1) &amp;&amp; 
\_\_\_\_\_\_\_\_\_\_\_\_(valueNew > list[indexNew+1])) {
 \_\_\_\_\_\_\_\_\_\_\_\_list[indexNew] = list[indexNew+1];
\_\_\_\_\_\_\_\_\_\_\_\_ indexNew++;
 \_\_\_\_\_\_\_\_}
 \_\_\_\_list[indexNew] = valueNew;
\_\_\_\_ pos--;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Is insertion sort stable ?

A

dunno google it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is selection sort stable ?

A

dunno google it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Bubble Sort:

A

Bubble sort is one of the simplest algorithms for sorting a list but also one of the slowest.

Theoretically speaking it has the same efficiency of selection sort

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write a Bubble sort algorithm :

A
void bubbleSort(int list[]) {
 \_\_\_\_for (int i = list.length-1; i>0; i--){
 \_\_\_\_\_\_\_\_for (int pos = 0; pos < i; pos++) {
\_\_\_\_\_\_\_\_\_\_\_\_ if (list[pos] > list[pos+1]) {
 \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_swap(list,pos,pos+1);
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_}
\_\_\_\_\_\_\_\_\_\_\_\_}
\_\_\_\_}
}
17
Q

Write a Bubble sort algorithm :

A
void bubbleSort(int list[]) {
 \_\_\_\_for (int i = list.length-1; i>0; i--){
 \_\_\_\_\_\_\_\_for (int pos = 0; pos < i; pos++) {
\_\_\_\_\_\_\_\_\_\_\_\_ if (list[pos] > list[pos+1]) {
 \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_swap(list,pos,pos+1);
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_}
\_\_\_\_\_\_\_\_\_\_\_\_}
\_\_\_\_}
}
18
Q

Bubble sort Worst case :

A

THETA(n^2)

19
Q

Is bubble sort stable ?

A

dunno

20
Q

Quick sort average performance?

A

nlogn

21
Q

Explain Quick sort

A

It was invented by the English Scientist C.A.R. Hoare in 1961.

It is a divide and conquer algorithm.

This means that it conforms to the basic idea of:
• Solving the subproblems recursively
• And then combining the solution into a solution for the larger instance

22
Q

Who is Hoare?

A

Hoare is principal scientist at Microsoft Research in the UK

23
Q

Why is Quick sort one of the most popular sorting algorithms ?

A
  • it is not difficult to implement

* it works well in a variety of situations – it is a good general purpose algorithm

24
Q

Advantages of Quicksort ?

A
  • It is in-place and uses only a small stack as auxiliary structure (if recursion is used the stack is implicit)
  • It works in O(n log n) in the average case
25
Q

Drawbacks of Quicksort?

A
  • It is recursive (i.e. more costly)
  • Implementation is more complicated without recursion
  • It takes O(n2) in the worst case
  • It is fragile.
26
Q

Steps of quicksort:

A

• Divide the list to be sorted into into two smaller sublists.

Conquer the smaller sublists either by calling the recursion for them or solving them with a straightforward approach if they are small enough

Combine the sorted lists of the subproblems into the solution for the large (original) problems

27
Q

What does it mean that quick sort is fragile ?

A

Difficult to improve or change because it might make the algorithm slower