Câu hỏi phỏng vấn Sorting
Câu hỏi

What is "stability" in sorting algorithms and why is it important?

Câu trả lời

Stability is an important property of sorting algorithms that ensures equal elements are sorted in the same order as they appear in the input[1][2]. In other words, if two elements have the same value, a stable sorting algorithm will sort them in the same order as they appear in the input data[1].

Unstable sorting algorithms, on the other hand, may rearrange equal elements[1]. For example, if a list of names is sorted alphabetically, a stable sorting algorithm will preserve the relative order of names that start with the same letter[2].

Stability is important in several scenarios[2][3]:

  1. Preserving order over multiple sorts: If a dataset is sorted by multiple keys, stability ensures that the order is preserved for each sort operation.

  2. Implementing complex sorting: Stable sorting algorithms can be used to implement sorting based o...

middle

middle

Gợi ý câu hỏi phỏng vấn

middle

When is Quicksort better than Mergesort?

junior

What is meant by to "Sort in Place"?

middle

When is each Sorting algorithm used?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào