Insetion Sort
private static int[] Insetion(int data[]) {
int temp;
int j;
for(int i = 1; i < data.length; i ++){
int tmp = data[i]; // tmp is want to be compared value
j = i - 1;
while( j > -1 && tmp < data[j]){ //if i is minimum , j will be a -1 , if arr[n] > arr[n+1]
data[j+1] = data[j]; // the latter turn to former
j--;
}
data[j+1] = tmp; // insert the Originally value (i value)
}
return data;
}
int temp;
int j;
for(int i = 1; i < data.length; i ++){
int tmp = data[i]; // tmp is want to be compared value
j = i - 1;
while( j > -1 && tmp < data[j]){ //if i is minimum , j will be a -1 , if arr[n] > arr[n+1]
data[j+1] = data[j]; // the latter turn to former
j--;
}
data[j+1] = tmp; // insert the Originally value (i value)
}
return data;
}
留言
張貼留言