Shaker sequence – algorithm
1. bubbles are sorted in both directions, and the bubble sort is left-to-right first. Let's make the bubble sort move from right to left, so that a sort order is completed.
2. Use Left with the Right Two flags to record the position of the ordered elements on the left and right sides.
An example of a sort is seen below:
Before sorting: 45 19 77 81 13 28 18 1977 11
Sort right: 19 45 77 13 28 18 19 7711 [81]
Sort left: [11] 19 45 77 13 28 1819 77 [81]
Sort right: [11] 19 45 13 28 18 19[77 77 81]
Sort left: [11 13] 19 45 18 28 19[77 77 81]
Sort right: [11 13] 19 18 28 19 [4577 77 81]
Sort left: [11 13 18] 19 19 28 [4577 77 81]
Sort right: [11 13 18] 19 19 [28 4577 77 81]
Sort left: [11 13 18 19 19] [28 4577 77 81]
As you can see above, the brackets indicate that the left and right sides have been sorted, and when you > right. The sorting is complete.
Sourcecodes
int shakersort (int a[],int lens) {int left=0,right=lens-1,shift;int i;while (left<right) {for (i=left;i<right;i++ {if (a[i]>a[i+1]) {swaper (a[i],a[i+1]); shift=i;}} right = Shift;for (i=right-1;i>=left;i--) {if (A[i+1]<a[i]) {swaper (a[i],a[i+1]); shift=i+1;}} Left=shift;} return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
[6] Algorithm path-bidirectional bubbling sort of shaker