Do I need to compare sorting with all neighboring cells?

Do I need to compare sorting by the largest value of A [i] and A [i + 1]? I think any sorting sort should, but I'm not sure. I checked the merge, insertion sorting and quicksort, and in each of them you need to compare the largest values ​​of A [i] and A [i + 1].

+3
source share
8 answers

Each correct algorithm must compare neighboring cells if they are not equal. Evidence. Assume otherwise. A [i] and A [i + 1] in the final array were not compared (A [i] <A [i + 1). What happens if their positions are replaced by the original array? All comparisons made by the algorithm give the same results as in the original run (*), therefore it performs the same permutation, therefore their end positions are swapped, which makes the algorithm incorrect.

(*) This follows from the fact that A [i] and A [j] are adjacent.

+2
source

Yes, in any comparison comparison, neighboring cells will always be compared with each other. See this page for more precise definitions of the lower bounds of sorting sorting.

+1
source

, , . , , . - .

A[i] > A[j] and A[j] > A[k] implies A[i] > A[k].

, .

+1

Shell . (, , ).

+1

Quicksort Mergesort . , , . , O (nlog n).

0

, . , . 2 A B, A , B. . A B .

, - , .

0

( --n-log-n) "" () - , , .

The idea is very similar to the more general "sort collations that require O (n log n) comparisons," so I would suggest that you either saw this recently or are going to cover it in your class.

0
source

because of my english, sorry .. i don't influence english.

@see Animated Sorting Algorithms: http://vision.bc.edu/~dmartin/teaching/sorting/anim-html/all.html

This site shows a comparison of 8 search algorithms: Insert · Select · Bubble · Shell · Merge · Heap · Fast · Quick3

0
source

Source: https://habr.com/ru/post/1698929/


All Articles