# Data Structures: Question Set – 12

#### What is a sorting algorithm?

A sorting algorithm is a process that places items in an array or list in a specific order, such as ascending or descending.

#### What are some common sorting algorithms?

A few popular sorting algorithms are as follows:

• Bubble sort
• Selection sort
• Insertion sort
• Merge sort
• Quick sort
• Heap sort

#### What is the time complexity of a sorting algorithm?

A sorting algorithm’s time complexity measures how long it takes to sort a given list or array. It is frequently stated in terms of how many comparisons or swaps are necessary to order the list. A sorting algorithm’s time complexity can range from O(n log n) for quick sort and merge sort to O(n2) for less effective methods like bubble sort and selection sort.

#### How do you pick the ideal sorting algorithm for the task at hand?

The most effective sorting algorithm for a particular task relies on a number of variables, including the amount of the input, the nature of the data being sorted, and the resources available (e.g., memory, processing power). While simpler algorithms like bubble sort and selection sort may be appropriate for smaller datasets or where memory utilisation is an issue, efficient algorithms like merge sort and quick sort are typically preferred for large datasets. When sorting integers or other data types with a constrained range of values, radix sort is frequently employed.

#### What distinguishes stable sorting algorithms from unstable?

When equal elements in a list are sorted, the relative order of the elements must be preserved. A stable sorting algorithm, for instance, will always maintain the same relative order of two entries in a list that have the same value. On the other side, an unstable sorting algorithm might alter the relative order of similar elements. An unstable sorting algorithm might change the locations of two elements with the same value in a list, for instance. When the relative order of similar elements matters, stable sorting algorithms are frequently preferred.

#### What does selection sort mean?

A simple sorting technique called selection sort involves finding the minimum element from an array’s unsorted portion repeatedly and replacing it with the array’s first unsorted member.

#### What is the selection sort’s time complexity?

Selection sort has an O(n2) time complexity, where n is the array’s total number of entries. Because of this, it is a poor algorithm for huge arrays.

#### How does a selection sort operate?

Selection sort divides the input array into two sections: one that is sorted and the other that is not. The unsorted half initially has all the items, but the sorted part is empty. Selection sort adds the minimum element from the unsorted part to the sorted part by swapping it with the unsorted part’s initial element throughout each iteration. The array gets sorted completely once this operation is finished.

#### Give an example of how selection sort operates.

A selection sort example utilising the input array [55, 33, 44, 22, 11] is provided below:

Pass 1: As element 11 is the minimum, it is swapped with element 55 to produce [11, 33, 44, 22, 55]

Pass 2: As element 22 is the minimum, it is swapped with element 33 to produce [11, 22, 44, 33, 55]

Pass 3: As element 33 is the minimum, it is swapped with element 44 to produce [11, 22, 33, 44, 55]

Pass 4: As element 44 is the minimum, and is in the correct position

Pass 5: As element 55 is the minimum, and is in the correct position

The sorted array’s final values are [11, 12, 22, 25, 64].

#### What are some of the benefits and drawbacks of selection sorting?

A selection sort’s simplicity and convenience of use are among its benefits. Its inefficiency for big arrays and the fact that it always performs n swaps even when the array is already sorted are drawbacks.