Sorting Algorithms

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/62

flashcard set

Earn XP

Description and Tags

Sorting

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

63 Terms

1
New cards

What is a sorting algorithm?

An algorithm that arranges elements of a list or array in a specific order (ascending or descending).

2
New cards

What are the main types of sorting algorithms?

Comparison-based and non-comparison-based sorting.

3
New cards

What are examples of comparison-based sorting algorithms?

Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort, Heap Sort.

4
New cards

What are examples of non-comparison-based sorting algorithms?

Counting Sort, Radix Sort, Bucket Sort.

5
New cards

What is the average time complexity of Bubble Sort?

O(n²)

6
New cards

What is the average time complexity of Insertion Sort?

O(n²)

7
New cards

What is the average time complexity of Selection Sort?

O(n²)

8
New cards

What is the average time complexity of Merge Sort?

O(n log n)

9
New cards

What is the average time complexity of Quick Sort?

O(n log n)

10
New cards

What is the average time complexity of Heap Sort?

O(n log n)

11
New cards

What is the average time complexity of Counting Sort?

O(n + k), where k is the range of input.

12
New cards

What is the average time complexity of Radix Sort?

O(nk), where k is the number of digits.

13
New cards

What is the best-case time complexity of Insertion Sort?

O(n), when the input is already sorted.

14
New cards

Which sorting algorithm is best for nearly sorted data?

Insertion Sort

15
New cards

What is the worst-case time complexity of Quick Sort?

O(n²), when the pivot divides the array unevenly.

16
New cards

Which sorting algorithm uses divide and conquer?

Merge Sort and Quick Sort

17
New cards

Which sorting algorithm is stable and has O(n log n) time?

Merge Sort

18
New cards

What is a stable sorting algorithm?

An algorithm that maintains the relative order of equal elements.

19
New cards

Is Quick Sort a stable algorithm?

No

20
New cards

Is Merge Sort stable?

Yes

21
New cards

Is Heap Sort stable?

No

22
New cards

Is Counting Sort stable?

Yes

23
New cards

What is the space complexity of Merge Sort?

O(n), due to auxiliary array.

24
New cards

What is the space complexity of Quick Sort?

O(log n), for recursion stack.

25
New cards

What is the space complexity of Bubble Sort, Insertion Sort, and Selection Sort?

O(1), as they are in-place sorting algorithms.

26
New cards

What is the concept of divide and conquer?

Divide the problem into subproblems, solve them recursively, and combine the results.

27
New cards

Which algorithm builds a max-heap to sort elements?

Heap Sort

28
New cards

Which algorithm chooses a pivot element and partitions the array?

Quick Sort

29
New cards

What is the pivot in Quick Sort?

An element used to divide the array into parts smaller and greater than itself.

30
New cards

What is the main advantage of Quick Sort?

Faster in practice for large datasets and can be done in-place.

31
New cards

Which sorting algorithm repeatedly swaps adjacent elements if they are in the wrong order?

Bubble Sort

32
New cards

Which sorting algorithm selects the smallest element and places it at the front?

Selection Sort

33
New cards

Which algorithm inserts elements into their correct position one at a time?

Insertion Sort

34
New cards

What is a non-comparison-based sorting algorithm?

An algorithm that doesn’t compare elements but uses properties like digit or frequency.

35
New cards

Which sorting algorithm is efficient for small arrays?

Insertion Sort

36
New cards

Which algorithm performs well on large datasets with uniform distribution?

Radix Sort or Merge Sort

37
New cards

What is bucket sort?

A sorting technique that distributes elements into buckets and sorts them individually.

38
New cards

Is bucket sort stable?

It can be, if stable sort is used within buckets.

39
New cards

Which sorting algorithm is best for linked lists?

Merge Sort

40
New cards

Why is Quick Sort not ideal for linked lists?

Due to poor partitioning and random access issues in linked lists.

41
New cards

Which sorting algorithm has consistent O(n log n) performance?

Merge Sort and Heap Sort

42
New cards

Why is Counting Sort not suitable for large ranges?

Because it requires extra space proportional to the range of values.

43
New cards

Which sorting algorithms are commonly used in libraries?

Merge Sort, Quick Sort, TimSort (a hybrid of both).

44
New cards

What is TimSort?

A hybrid sorting algorithm based on Merge Sort and Insertion Sort, used in Java and Python standard libraries.

45
New cards

What is the difference between stable and unstable sort?

Stable keeps the order of equal elements; unstable may not.

46
New cards

Which algorithm is preferred when memory is limited?

Heap Sort, as it's in-place and doesn't need extra memory.

47
New cards

Why might Merge Sort be preferred over Quick Sort?

It is stable and guarantees O(n log n) time complexity.

48
New cards

Why might Quick Sort be preferred over Merge Sort?

It usually outperforms Merge Sort in practice due to in-place sorting and better cache usage.

49
New cards

What is shell sort?

An improved version of insertion sort using gaps to compare and move elements.

50
New cards

Is shell sort stable?

No

51
New cards

What is the best sorting algorithm for large, unsorted datasets in general?

Quick Sort or Merge Sort, depending on constraints.

52
New cards

What is the main drawback of Bubble Sort?

Very slow on large datasets due to O(n²) time.

53
New cards

What is the key idea behind Counting Sort?

Counting the number of occurrences of each unique value.

54
New cards

Is Radix Sort stable?

Yes, if the stable sort is used at each digit.

55
New cards

Does Radix Sort work with negative numbers?

Only if modified, otherwise it works with non-negative integers.

56
New cards

What kind of data is Radix Sort suitable for?

Fixed-length numeric or string keys.

57
New cards

How does Bucket Sort work?

Divides data into a fixed number of buckets, sorts each bucket, then combines them.

58
New cards

Which sorting algorithm is based on binary heap?

Heap Sort

59
New cards

What is heapify in Heap Sort?

The process of maintaining the heap property in a subtree.

60
New cards

What is the time complexity to build a heap?

O(n)

61
New cards

What is the complexity of heapify operation?

O(log n)

62
New cards

Which sorting algorithm is used in Java’s Arrays.sort() for primitives?

Dual-pivot Quick Sort.

63
New cards

Which sorting algorithm is used in Java’s Arrays.sort() for objects?

TimSort.