이 블로그 검색

2023년 3월 17일 금요일

The difference between stable sort and unstable sort

Definition

When sorting a list, the resulting values may be the same, but the difference in the internal sorting process creates a distinction between stable and unstable sorting methods.

Stable sorting preserves the original position of elements in the list while sorting. Unstable sorting does not preserve the original position of elements in the list while sorting.

Example

Consider the following list: [A, C, A, B, C, D]. When sorting this list in lexicographic order, it would result in [A, A, B, C, C, D]. However, it is possible to distinguish between the first occurrence of A and the second occurrence of A by assigning a unique index to each occurrence:

Given sort:

[A(1), C(1), A(2), B, C(2), D]

Stable sort:

[A(1), A(2), B, C(1), C(2), D]

Unstable sort:

[A(2), A(1), B, C(1), C(2), D]

Examples of stable sorting methods

  • Bubble Sort
  • Insertion Sort
  • Merge Sort
  • Counting Sort
  • Bucket Sort
  • Radix Sort

Examples of unstable sorting methods

  • Selection sort
  • Heap Sort
  • Shell Sort
  • Quick Sort

댓글 없음:

댓글 쓰기

Logic Gate Truth Tables & Definitions

Logic Gate Truth Tables Java Code !A // NOT A&B // AND ~(A&B) // NAND A|B // OR ~(A|B) // XOR A^B // XOR ~(A^B) // XNOR ~A // Inve...