Created
December 24, 2015 09:52
-
-
Save Yur-ok/3a99c3a0903db5205367 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Lesson3.KeyPoint3; | |
import java.util.Arrays; | |
/** | |
* Created by Юрий on 23.12.2015. | |
*/ | |
public class SortUseSwapMinTimes { | |
public static void main(String[] args) { | |
int[] arr = {4, -2, 0, 5, 3, 1}; | |
sort(arr); | |
} | |
static void sort(int[] data) { | |
if (data == null || data.length == 0) { | |
System.out.println(-1); | |
return; | |
} | |
int len = data.length - 1; | |
for (int idx = 0; idx < data.length - 1; idx++) { | |
for (int k = 0; k < len; k++) { | |
if (data[k] < data[k + 1]) { | |
continue; | |
} | |
swap(data, k, k + 1); | |
System.out.println(Arrays.toString(data)); | |
} | |
len--; | |
} | |
} | |
static void swap(int[] data, int i, int k) { | |
int temp; | |
if (data[i] > data[k]) { | |
temp = data[i]; | |
data[i] = data[k]; | |
data[k] = temp; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Той оптимизации, что требовалось здесь нет.