package com.arithmetic.test;
public class BubbleSort {
/*
* 无参构造方法
*/
public BubbleSort() {
}
/*
* 冒泡排序法
*/
public int[] bubbleSort(int[] array){
int temp;
for (int i = 1; i <= array.length; i++) {
for (int j = 0; j < array.length-i; j++) {
if (array[j]>array[j+1]) {
temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
return array;
}
public static void main(String[] args) {
int[] arr ={12,34,56,778,999,445,33,900,34,223,24,567,788,77};
System.out.print("排序前:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
arr = new BubbleSort().bubbleSort(arr);
System.out.print("排序后:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
package com.arithmetic.test;
public class SelectSort {
public SelectSort() {}
/*
* 选择排序方法
*/
public int[] selectSort(int [] arr){
int minIndex;
int temp;
for (int i = 0; i < arr.length-1; i++) {
minIndex = i;
for (int j = i+1; j < arr.length; j++) {
if (arr[minIndex]>arr[j]) {
temp = arr[j];
arr[j] = arr[minIndex];
arr[minIndex]=temp;
}
}
}
return arr;
}
public static void main(String[] args){
int[] arr ={1,34,56,778,999,445,33,900,34,223,24,567,788,77};
System.out.print("排序前:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
arr = new SelectSort().selectSort(arr);
System.out.print("排序后:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
(2)Java代码实现:
package com.arithmetic.test;
public class InsertSort {
public InsertSort() {
}
/*
* 插入排序
*
*/
public int[] insertSort(int[] arr){
for (int i = 1; i < arr.length; i++) {
//value为当前要插入的值
int value = arr[i];
int j=i-1;
//在当前值之前已经排列好的数组中依次对比,如果值都比当前值大,则将当前值后移一位,直到比当前值小或者没有数值为止
while (j>=0&&arr[j]>value) {
arr[j+1] = arr[j];
j--;
}
//将当前要插入的值插入
arr[j+1] = value;
}
return arr;
}
public static void main(String[] args) {
int[] arr ={1,34,56,778,999,445,33,900,34,223,24,567,788,77};
System.out.print("排序前:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
arr = new InsertSort().insertSort(arr);
System.out.print("排序后:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
package com.arithmetic.test;
public class MergeSort {
public MergeSort() {}
/*
* 归并排序方法
*/
/* public void Sort(int[] arr){
mergeSort(arr, 0, arr.length-1);
}*/
public void mergeSort(int[] arr,int low,int height){
if (low
package com.arithmetic.test;
public class QuickSort {
public QuickSort() {}
public int[] quickSort(int[] arr,int low,int height){
//判斷初始的低位是否比高位低
if (low=base) {
height--;
}
//將高位值放到低位也就是左邊
if (low
package com.arithmetic.test;
public class ShellSort {
public ShellSort() {}
/*
* 希尔排序方法
*/
public void shellSort(int[] arr){
int step = arr.length/2;
while (step>=1) {
for (int i = step; i < arr.length; i++) {
//需要插入的值
int value = arr[i];
//前一个插入值的索引下标
int j =i-step;
//如果存在前一个值,并且大于需要插入的值
while (j>=0&&arr[j]>value) {
//就将前一个插入的值后移一位
arr[j+step]=arr[j];
//循环对比判断插入当前值之前插入值得大小,并作出相应的处理
j-=step;
}
//arr[i]=value;
//这里不能用arr[i]=value虽然从一开始j =i-step看起来j+step=i,但是后面在内层循环中j是在变化的,而i没变
arr[j+step] = value;
}
//步长缩小半
step=step/2;
}
}
public static void main(String[] args) {
int[] arr ={1,34,56,778,999,445,33,900,34,223,24,567,788,77};
System.out.print("排序前:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
new ShellSort().shellSort(arr);
System.out.print("排序后:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}