1、简单选择排序
思想:每次排序找出最小的值,放在最前面。并不是在一次排序中,每当发现比目标位置小的元素就交换。而是每次排序只有一次交换。
import java.util.Arrays;
public class test01 {
public static void main(String[] args) {
int a[] = {2, 9, 34, 6, 32, 45, 64, 32};
for (int i = 0; i < a.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < a.length; j++) {
if (a[minIndex] > a[j]) {
minIndex = j;
}
}
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
System.out.println(Arrays.toString(a));
}
}
字符串数组排序 ,以简单选择排序为基础:
import java.util.Arrays;
public class HomeWork01 {
public static void main(String[] args) {
String array[] = {"fcf", "af", "ed", "cd", "cf", "vf", "sx"};
boolean isMatchIf = false;
for (int i = 0; i < array.length; i++) {
for (int m = 0; m < array.length - 1; m++) {
isMatchIf = false;
char[] temp1 = array[m].toCharArray();
char[] temp2 = array[m + 1].toCharArray();
int length = temp1.length > temp2.length? temp2.length : temp1.length;
for (int n = 0; n < length; n++) {
if (temp1[n] > temp2[n]) {
String tempString = array[m];
array[m] = array[m + 1];
array[m + 1] = tempString;
isMatchIf = true;
break;
}else if(temp1[n] != temp2[n]){
break;
}
}
if(!isMatchIf && temp1.length > temp2.length){
String tempString = array[m];
array[m] = array[m + 1];
array[m + 1] = tempString;
}
}
}
System.out.println(Arrays.toString(array));
}
}
2、冒泡排序
思想:每次排序,从第一位与相邻的一位比较,符合条件就换位,注意每次排序内层循环的长度。
import java.util.Arrays;
public class test01 {
public static void main(String[] args) {
int a[] = {2, 9, 34, 6, 32, 45, 64, 32};
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(a));
}
}
3、插入排序
int[] a = {34, 67, 32, 2, 56, 46, 45, 21, 46, 43, 32, 78, 54, 32, 7, 78};
int[] a = {34, 67, 32, 2, 56, 46, 45, 21, 46, 43, 32, 78, 54, 32, 7, 78};
public static void inserSort(int[] a){
for (int i = 1; i < a.length; i++) {
int temp = a[i];
int j;
for ( j = i - 1; j >= 0; j--) {
if (a[j] > temp) {
a[j + 1] = a[j];
}else {
break;
}
}
a[j + 1] = temp;
}
System.out.println(Arrays.toString(a));
}
4、快速排序
1.设置两个变量i,j,排序开始从i = 1, j = n;
2.将第一个元素作为关键数据,赋值给 x,即 x = a[1];
3. 从j开始向前搜索,即由后开始向前搜索,找到第一个小于 x的值,两者交换;
4.从i开始向后搜索,即由前开始往后搜索,找到第一个大于x的值,两者交换;
5,重复第3.4部,直到i=j;
eg:
49, 38, 65, 97, 76, 13, 27;
步骤:
1. x = 49,
进行第一次交换:27,38,65,97,76,13,49;(j)
第二次交换:27,38,49,97,76,13,65(i)
第三次交换:27,38,13,97,76,49,65(j)
第四次交换:27,38,13,49,76,97,65(i)
第五次交换:i=j;结束一躺快速排序
现在以49为关键数据为中心,49前面的数据都要比49小,49后面的数据都要比49大。
然后再对这2组数据进行快速排序。
/**
*
* @return
*/
public static int getMiddle(int [] a, int low, int high){
int temp = a[low]; //初始化中轴的位置
while(low < high){
while(low < high && a[high] >= temp){
high --;
}
a[low] = a[high]; // 比中轴小的放在低位
while(low < high && a[low] <= temp){
low ++;
}
a[high] = a[low]; //比中轴大的放在高位
}
a[low] = temp;
return low;
}
public static void quickSort(int [] a, int low, int high){
if(low < high){
int middle = getMiddle(a, low, high); //把数组分成两半
quickSort(a, low, middle - 1);
quickSort(a, middle + 1, high);
}
}
5、希尔排序
代码一:
//希尔排序
public static void shellSort(int[] a) {
int d = a.length;
while (true) {
int temp;
d = (int) Math.ceil(d / 2);
for (int x = 0; x < d; x++) {
for (int i = x + d; i < a.length; i += d) {
temp = a[i];
int j;
for (j = i - d; j >= 0 && a[j] > temp; j -= d) {
a[j + d] = a[j];
}
a[j + d] = temp;
}
}
if (d == 1) {
break;
}
}
}
代码二:
/**
* 3、用插入排序和希尔排序实现
* int a = {2,3,4,1,5,7,9,10} 的排序。(不要用以前写好的工具类)
*/
public static void main(String[] args) {
int[] array = {2, 3, 4, 1, 5, 7, 9, 10};
shellSort(array);
System.out.println(Arrays.toString(array));
}
public static void shellSort(int[] array){
int temp= array.length;
for (int gap = temp / 2; gap > 0; gap /= 2) {
for (int i = 0; i < gap; i++) {
for (int j = i + gap; j < temp; j += gap) {
if (array[j] < array[j - gap])
{
int result = array[j];
int k = j - gap;
while (k >= 0 && array[k] > result)
{
array[k + gap] = array[k];
k -= gap;
}
array[k + gap] = result;
}
}
}
}
}
6、归并排序
/**
* 归并排序
* @param a
* @return
*/
public static int [] mergeSort(int [] a){
//如果待排序的数组就只有一个元素,那么直接返回
if(a.length < 2){
return a;
}
int middle = a.length / 2;
int left [] = Arrays.copyOfRange(a, 0, middle);
int right [] = Arrays.copyOfRange(a, middle, a.length);
//递归
left = mergeSort(left);
right = mergeSort(right);
return merge(left,right);
}
private static int[] merge(int[] left, int[] right) {
int [] result = new int [left.length + right.length];
int index = 0;
while(left.length > 0 && right.length > 0){
if(left [0] > right [0]){
//把比较后小的那个值放入目标数组
result[index ++] =right[0];
//从原来数组中去掉选中的元素
right = Arrays.copyOfRange(right, 1, right.length);
}else{
//把比较后小的那个值放入目标数组
result[index ++] =left[0];
//从原来数组中去掉选中的元素
left = Arrays.copyOfRange(left, 1, left.length);
}
}
//如果比较完成后原始数组中还有值
if(left.length > 0){
System.arraycopy(left, 0, result, index, left.length);
}else if(right.length > 0){
System.arraycopy(right, 0, result, index, right.length);
}
return result;
}