4种交换顺序以及5种排序
1.冒泡排序
2.选择排序
3.插入排序
4.快速排序
5.归并排序(分治)
6.二分法查找
经测试均成功。。。
话不多说 直接上代码
package l.m.sort;
import java.util.Arrays;
public class Test {
//4种交换顺序
//1--异或^
@org.junit.Test
public void test1(){
int a=10;
int b=20;
//a=1011 b=0111
//a=1100
//b=1011
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println(a);
System.out.println(b);
}
@org.junit.Test
public void test2(){
int a=10;
int b=20;
a=b+(b=a)*0;
System.out.println(a);
System.out.println(b);
}
@org.junit.Test
public void test3(){
int a=10;
int b=20;
a=a+b;
b=a-b;
a=a-b;
System.out.println(a);
System.out.println(b);
}
//---------------------------------------------------------------------------------
//1.冒泡排序
@org.junit.Test
public void test4() {
int[] array = {34, 2, 45, 10, 14, 23, 56, 89, 8, 19};
int temp;
for (int i=0;i<array.length;i++){
for (int j=0;j<array.length-i-1;j++){
if (array[j]>array[j+1]){
temp = array[j];
array [j] = array[j+1];
array[j+1] = temp;
}
}
}
for(int i:array){
System.out.print (i+" ");
}
}
//---------------------------------------------------------------------------------
//2.选择排序
@org.junit.Test
public void test5() {
int[] array = {34, 2, 45, 10, 14, 23, 56, 89, 8, 19};
int temp ;
for(int i=0;i<array.length;i++){
int minIndex = i;
for (int j=i;j<array.length;j++){
if(array[j]<array[minIndex]){
minIndex=j;
}
}
temp=array[minIndex];
array[minIndex]=array[i];
array[i]=temp;
}
for(int i:array){
System.out.print (i+" ");
}
}
//---------------------------------------------------------------------------------
//3.插入排序
@org.junit.Test
public void test6() {
int[] array = {34, 2, 45, 10, 14, 23, 56, 89, 8, 19};
int temp;
int j;
//34,2
//2,34,45,10
for (int i = 0; i < array.length - 1; i++) {
//j=2
temp = array[i + 1];
for(j=i;j>=0;j--){
if(array[j]>temp){
array[j+1]=array[j];
}else{
break;
}
}
array[j+1]=temp;
}
for(int i:array){
System.out.print (i+" ");
}
}
//---------------------------------------------------------------------------------
// 4.快速排序
// tips:基准
@org.junit.Test
public void test7() {
int[] array = { 34, 2, 45, 10, 14, 23, 56, 89, 8, 19 };
test8(array, 0, array.length - 1);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public void test8(int[] array, int begin, int end) {
if (begin >= end) {
return;
}
int i = begin;
int j = end;
int key = array[i];
//当i=j时不循环
while (i < j) {
while (array[j] >= key && i < j) {
j--;
}
array[i] = array[j];
while (array[i] <= key && i < j) {
i++;
}
array[j]=array[i];
}
array[i]=key;
test8(array, begin, i - 1);
test8(array, i + 1, end);
}
//---------------------------------------------------------------------------------
// 5.归并排序(分治)
// tips:
// Arrays.copyOfRange(aa[ ] original,int from,int to)
// 将一个原始的数组original,从下标from开始复制,复制到上标to,生成一个新的数组。
// 注意这里包括下标from,不包括上标to。
@org.junit.Test
public void test9() {
int[] array = { 34, 2, 45, 10, 14, 23, 56, 89, 8, 19 };
int[] result = test10(array);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
}
public int[] test10(int[] array) {
if (array.length < 2) {
return array;
}
int middle = array.length / 2;
int[] left = Arrays.copyOfRange(array, 0, middle);
int[] right = Arrays.copyOfRange(array, middle, array.length);
return test11(test10(left),test10(right));
}
public int[] test11(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
for (int index = 0, i = 0, j = 0; index < result.length; index++) {
if (i >= left.length) {
result[index] = right[j];
j++;
}else if (j >= right.length) {
result[index] = left[i];
i++;
//比较值的大小
}else if (left[i] > right[j]) {
result[index] = right[j];
j++;
}else {
result[index] = left[i];
i++;
}
}
return result;
}
//---------------------------------------------------------------------------------
// 6.二分法查找(通过数字查索引)
// tips:数据量大采用;数据有序不重复
@org.junit.Test
public void test12() {
int[] array = { 2, 8, 10, 14, 19, 23, 34, 45, 56, 89 };
int index = test13(array, 14);
System.out.println("index = " + index);
}
public int test13(int[] array, int value) {
int begin = 0;
int end = array.length - 1; // 9
int middle;
while (begin <= end) {
middle = (begin + end) / 2;// 4
if (value < array[middle]) {// 56<array[4] 56<14
end = middle - 1;// 3
} else if (value > array[middle]) {
begin = middle + 1;// 3
} else {
return middle;
}
}
return -1;
}
}