规则:
//冒泡排序法
public class BubbleSort {
public static void main(String[] args) {
int[] array = new int[]{5,7,8,6,9,10,1,2,3,4};
boolean flag = false;
for (int i = 0;iarray[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag = true;
}
}
if (!flag){
break;
}
}
for (int i =0;i
规则:
//选择排序
public static void selectSort(int[] arr){
for (int i=0;i arr[j]) {
min = arr[j];
minIndex = j;
}
}
if (minIndex != i){
arr[minIndex] = arr[i];
arr[i] = min;
}
}
}
// 插入排序
public static void insertSort(int[] array){
int j;
for (int i = 1;i0 && temp < array[j-1];j--){
array[j] = array[j-1];
}
array[j] = temp;
}
for (int i = 0;i
//归并排序
public class MergetSort {
public static void main(String[] args) {
int[] array = new int[]{5,7,8,6,0,1,2,9,3,4};
int temp[] = new int[array.length];
mergeSort(array,0,array.length-1,temp);
System.out.println(Arrays.toString(array));
}
//归并排序
public static void mergeSort(int[] arr,int left,int right,int[] temp){
if (left < right){
int mid = (left + right) / 2;
mergeSort(arr,left,mid,temp);
mergeSort(arr,mid + 1,right,temp);
merge(arr,left,mid,right,temp);
}
}
public static void merge(int[] arr,int left,int mid,int right,int[] temp){
int i = left; //初始化i,左边有序序列的初始索引
int j = mid +1; //初始化j,右边有序序列的初始索引
int t =0; //指向temp当前的索引
/*
* (一)先把左右两边(有序)的数据按照规则填充到temp
* 直到左右两边的有序序列,有一边处理完为止
*
* */
while(i<=mid && j<=right){
if (arr[i] <= arr[j]){
temp[t] = arr[i];
t +=1;
i +=1;
}else {
temp[t] = arr[j];
t +=1;
j +=1;
}
}
/*
* (二)把剩余数据的一边的数据依次全部填充到temp
* */
while (i<=mid){ //左边有序序列还有剩余的元素,全部填充到temp
temp[t] = arr[i];
t +=1;
i +=1;
}
while (j <= right){
temp[t] = arr[j];
t +=1;
j +=1;
}
t = 0;
int tempLeft = left;
System.out.println("tempLeft = " + tempLeft + " right = " + right);
while (tempLeft <= right){
arr[tempLeft] = temp[t];
t +=1;
tempLeft +=1;
}
}
}
希尔排序是对插入排序的改进
//希尔排序(交换法)
public static void shellSort(int[] arr) {
int temp = 0;
for (int gap = arr.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < arr.length; i++) {
for (int j = i - gap; j >= 0; j -= gap) {
if (arr[j] > arr[j + gap]) {
temp = arr[j];
arr[j] = arr[j + gap];
arr[j + gap] = temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
//希尔排序(移位法)
public static void shellSort2(int[] arr){
//增量gap,逐步缩小增量
for (int gap = arr.length / 2; gap > 0; gap /= 2){
for (int i = gap; i < arr.length; i++) {
int j = i;
int temp = arr[j];
if (arr[j] < arr[j-gap]){
while (j - gap >= 0 && temp < arr[j - gap]){
arr[j] = arr[j-gap];
j -= gap;
}
arr[j] = temp;
}
}
System.out.println(Arrays.toString(arr));
}
}
快速排序是对冒泡排序的改进
//快速排序
public static void quickSort(int arr[],int left,int right){
int l = left; //左下标
int r = right; // 右下标
int pivot = arr[(left+right)/2]; //中间值
int temp = 0;
while (l pivot){
r -=1;
}
if (l >= r){
break;
}
temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
if (arr[l] == pivot){
r -=1;
}
if (arr[r] == pivot){
l +=1;
}
}
if (l == r){
l +=1;
r -=1;
}
if (leftl){
quickSort(arr,l,right);
}
}
//基数排序
public static void radixSort(int[] arr){
//1.得到数组中数的最大的位数
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max){
max = arr[i];
}
}
int maxLength = (max + "").length();
int[][] bucket = new int[10][arr.length];
int[] bucketElementCounts = new int[10];
for (int i = 0, n = 1; i < maxLength; i++, n *= 10) {
for (int j = 0; j < arr.length; j++) {
int digitOfElement = arr[j] / n % 10;
bucket[digitOfElement][bucketElementCounts[digitOfElement]] = arr[j];
bucketElementCounts[digitOfElement]++;
}
int index = 0;
for (int k = 0; k < bucketElementCounts.length;k++){
if (bucketElementCounts[k] != 0){
for (int l = 0; l < bucketElementCounts[k]; l++){
arr[index++] = bucket[k][l];
}
}
bucketElementCounts[k] = 0;
}
System.out.println(Arrays.toString(arr));
}
}
public class HeapSort implements IArraySort {
public int[] sort(int[] sourceArray) throws Exception {
// 对 arr 进行拷贝,不改变参数内容
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
int len = arr.length;
buildMaxHeap(arr, len);
for (int i = len - 1; i > 0; i--) {
swap(arr, 0, i);
len--;
heapify(arr, 0, len);
}
return arr;
}
private void buildMaxHeap(int[] arr, int len) {
for (int i = (int) Math.floor(len / 2); i >= 0; i--) {
heapify(arr, i, len);
}
}
private void heapify(int[] arr, int i, int len) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int largest = i;
if (left < len && arr[left] > arr[largest]) {
largest = left;
}
if (right < len && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
swap(arr, i, largest);
heapify(arr, largest, len);
}
}
private void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
你知道的越多,你不知道的越多。
有道无术,术尚可求,有术无道,止于术。
如有其它问题,欢迎大家留言,我们一起讨论,一起学习,一起进步