package com.foundation.sort;
/***
* 注:为什么今天会想到重新来写一下排序 还得研究执行的过程
* (今天下午华为的技术员进行复试,喊哥给他手写排序,
* 写了就算了,还让我给他说名执行过程,
* 说明过程写了就算,还得让你给他说出来,
* 在第几次的循环结果出来,我只能说,真的很挑剔)
* */
public class SortTest {
public static void main(String[] args) {
int[] arrays={56,67,2,45,32,1};
//chooseSort(arrays);
bubbleSort(arrays);
print(arrays);
}
/***选择排序*/
public static void chooseSort(int[] arrays) {
for (int i = 0; i < arrays.length; i++) {
//注释中的是用来观察选择排序,每一次排序结果的顺序
//System.out.print("第"+i+"次的排序结果:");
//print(arrays);
//System.out.println();
for (int j = i+1; j < arrays.length ; j++) {
if (arrays[i] > arrays[j]) {
int temp = arrays[i];
arrays[i] = arrays[j];
arrays[j] = temp;
}
}
}
/*
* 第0次的排序结果:56,67,2,45,32,1,
第1次的排序结果:1,67,56,45,32,2,
第2次的排序结果:1,2,67,56,45,32,
第3次的排序结果:1,2,32,67,56,45,
第4次的排序结果:1,2,32,45,67,56,
第5次的排序结果:1,2,32,45,56,67,
result:=1,2,32,45,56,67,
*/
}
/**冒泡排序*/
public static void bubbleSort(int[] arrays){
for (int i = 0; i < arrays.length; i++) {
//注释中的是用来观察选择排序,每一次排序结果的顺序
System.out.print("第"+i+"次的排序结果:");
print(arrays);
System.out.println();
for (int j = 0; j < arrays.length-1; j++) {
if(arrays[j]>arrays[j+1]){
int temp=arrays[j];
arrays[j]=arrays[j+1];
arrays[j+1]=temp;
}
}
}
/*
* 第0次的排序结果:56,67,2,45,32,1,
第1次的排序结果:56,2,45,32,1,67,
第2次的排序结果:2,45,32,1,56,67,
第3次的排序结果:2,32,1,45,56,67,
第4次的排序结果:2,1,32,45,56,67,
第5次的排序结果:1,2,32,45,56,67,
result:=1,2,32,45,56,67,
*
* */
}
public static void print(int[] arrays){
for (int i : arrays) {
System.out.print(i+",");
}
}
}
/***
* 注:为什么今天会想到重新来写一下排序 还得研究执行的过程
* (今天下午华为的技术员进行复试,喊哥给他手写排序,
* 写了就算了,还让我给他说名执行过程,
* 说明过程写了就算,还得让你给他说出来,
* 在第几次的循环结果出来,我只能说,真的很挑剔)
* */
public class SortTest {
public static void main(String[] args) {
int[] arrays={56,67,2,45,32,1};
//chooseSort(arrays);
bubbleSort(arrays);
print(arrays);
}
/***选择排序*/
public static void chooseSort(int[] arrays) {
for (int i = 0; i < arrays.length; i++) {
//注释中的是用来观察选择排序,每一次排序结果的顺序
//System.out.print("第"+i+"次的排序结果:");
//print(arrays);
//System.out.println();
for (int j = i+1; j < arrays.length ; j++) {
if (arrays[i] > arrays[j]) {
int temp = arrays[i];
arrays[i] = arrays[j];
arrays[j] = temp;
}
}
}
/*
* 第0次的排序结果:56,67,2,45,32,1,
第1次的排序结果:1,67,56,45,32,2,
第2次的排序结果:1,2,67,56,45,32,
第3次的排序结果:1,2,32,67,56,45,
第4次的排序结果:1,2,32,45,67,56,
第5次的排序结果:1,2,32,45,56,67,
result:=1,2,32,45,56,67,
*/
}
/**冒泡排序*/
public static void bubbleSort(int[] arrays){
for (int i = 0; i < arrays.length; i++) {
//注释中的是用来观察选择排序,每一次排序结果的顺序
System.out.print("第"+i+"次的排序结果:");
print(arrays);
System.out.println();
for (int j = 0; j < arrays.length-1; j++) {
if(arrays[j]>arrays[j+1]){
int temp=arrays[j];
arrays[j]=arrays[j+1];
arrays[j+1]=temp;
}
}
}
/*
* 第0次的排序结果:56,67,2,45,32,1,
第1次的排序结果:56,2,45,32,1,67,
第2次的排序结果:2,45,32,1,56,67,
第3次的排序结果:2,32,1,45,56,67,
第4次的排序结果:2,1,32,45,56,67,
第5次的排序结果:1,2,32,45,56,67,
result:=1,2,32,45,56,67,
*
* */
}
public static void print(int[] arrays){
for (int i : arrays) {
System.out.print(i+",");
}
}
}