-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcocktailShakerSort.c
More file actions
54 lines (51 loc) · 1.44 KB
/
Copy pathcocktailShakerSort.c
File metadata and controls
54 lines (51 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// Created by ali raz on 6/6/20.
//
#include <stdbool.h>
#include "swap.h"
/*
procedure cocktailShakerSort(A : list of sortable items) is
do
swapped := false
for each i in 0 to length(A) − 2 do:
if A[i] > A[i + 1] then // test whether the two elements are in the wrong order
swap(A[i], A[i + 1]) // let the two elements change places
swapped := true
end if
end for
if not swapped then
// we can exit the outer loop here if no swaps occurred.
break do-while loop
end if
swapped := false
for each i in length(A) − 2 to 0 do:
if A[i] > A[i + 1] then
swap(A[i], A[i + 1])
swapped := true
end if
end for
while swapped // if no elements have been swapped, then the list is sorted
end procedure
*/
void cocktailShakerSort(int arr[],int length){
bool isSwapped;
do{
isSwapped=false;
for (int i = 0; i < length-2; ++i) {
if(arr[i]>arr[i+1]){
swap(&arr[i],&arr[i+1]);
isSwapped=true;
}
}
if(!isSwapped){
break;
}
isSwapped=false;
for (int i = length-2; i > 0; i--) {
if (arr[i] > arr[i + 1]) {
swap(&arr[i], &arr[i + 1]);
isSwapped = true;
}
}
}while (isSwapped);
}