Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static long cocktailSort(int[] list) {
- int begin = -1;
- int end = list.length - 2;
- boolean swapped = false;
- int temp;
- do {
- swapped = false;
- begin += 1;
- for(int i = begin; i < end; i++) {
- if(list[i] > list[i + 1]) {
- temp = list[i];
- list[i] = list[i + 1];
- list[i + 1] = temp;
- swapped = true;
- }
- }
- if(!swapped) {
- end -= 1;
- for(int i = end; i > begin; i--) {
- if(list[i] > list[i + 1]) {
- temp = list[i];
- list[i] = list[i + 1];
- list[i + 1] = temp;
- swapped = true;
- }
- }
- }
- } while(swapped);
- return list;
- }
Advertisement
Add Comment
Please, Sign In to add comment