ruby
- def injection_sort
- a = [40,12,23,543,53,2,77,34]
- i = 1
- while(i<a.length)
- tmp = a[i]
- j = i-1
- while(tmp<a[j])
- a[j+1] = a[j]
- j = j-1
- break if (j == -1)
- end
- a[j+1] = tmp
- i=i+1
- end
- return a
- end
def injection_sort a = [40,12,23,543,53,2,77,34] i = 1 while(i<a.length) tmp = a[i] j = i-1 while(tmp<a[j]) a[j+1] = a[j] j = j-1 break if (j == -1) end a[j+1] = tmp i=i+1 end return a end
Java
- public int[] sort(String[] args) {
- int[] number = {40,12,23,543,53,2,77,34};
- for(int i = 1;i<number.length;i++){
- int tmp = number[i];
- int j = i-1;
- while(tmp<number[j]){
- number[j+1] = number[j];
- j--;
- if(j == -1){
- break;
- }
- }
- number[j+1] = tmp;
- }
- return number;
- }