-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays_Pancake_sort.java
More file actions
52 lines (44 loc) · 1.55 KB
/
arrays_Pancake_sort.java
File metadata and controls
52 lines (44 loc) · 1.55 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
import java.io.*;
import java.util.*;
// "static void main" must be defined in a public class.
public class Main {
// pancacke sort
static int[] pancakeSort(int[] arr) {
// we are gradually reducing range
for(int length = arr.length; length >=0; length--) {
int biggestValue = arr[0];
int biggestIndex = 0;
// looking for biggest in current range
for( int i = 0; i < length; i++){ // this range will be reduced (length)
if(arr[i] > biggestValue) {
biggestValue = arr[i];
biggestIndex = i;
}
}
// we put BIGGEST in front
arr = flip(arr,biggestIndex);
// we need it to don't tuch already flipped previous BIGGEST ONES
arr = flip(arr, length-1 );
}
return arr;
}
// flip function flips order in given range (first k elems) of array
static int[] flip(int [] arr , int k ){
int endIndex = k;
for(int i = 0 ; i <= k ; i ++, endIndex--){
if( endIndex != i && endIndex > i ) {
int swap = arr[i];
arr[i] = arr[endIndex];
arr[endIndex] = swap;
} else {
break;
}
}
return arr;
}
// execution
public static void main(String[] args) {
int[] arr = {1, 5, 4, 3, 2};
System.out.println( Arrays.toString( pancakeSort(arr) ) );
}
}