前言
本文章学习尚硅谷Java数据结构与java算法
即使你对java完全不了解也没有关系,只要你学习过c++或别的编程语言,一定能看懂的,一步一步实现算法排序,帮助你理解。
谢谢韩老师。
下面的代码有些是c++写的,有些是java写的。
其实差别不大,应该都能看懂。
selectSort
java
import java.util.Arrays;
//选择排序
//选出最小的数字赋值给第一位,以此类推
public class selectSort {
public static void main(String[] args) {
int[] arr={11,5,4,6,78,1,999};
SelectSort(arr);
}
public static void SelectSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int min = arr[i];
int minindex = i;
for(int j=i+1;j<arr.length;j++)
{
if(min>arr[j])
{
min=arr[j];
minindex=j;
}
}
arr[minindex]=arr[i];
arr[i]=min;
System.out.println("第"+(i+1)+"次"+Arrays.toString(arr));
}
}
}
insertSort
java
import java.util.Arrays;
public class insertSort {
public static void main(String[] args) {
int[] arr = { 12, 2, 45, 16 };
insertSort(arr);
}
public static void insertSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int insertVal = arr[i];
int insertindex = i - 1;
while (insertindex >= 0 && insertVal < arr[insertindex]) {
arr[insertindex + 1] = arr[insertindex];
insertindex--;
}
arr[insertindex + 1] = insertVal;
System.out.println("第" + i + Arrays.toString(arr));
}
}
}
希尔排序ShellSort
java
import java.util.Arrays;
//每次分一半对
public class ShellSort {
public static void main(String[] args) {
int[] arr = { 4, 7, 2, 1, 6, 5, 9, 8, 13 };
shellSort1(arr);
}
public static void shellSort(int[] arr) {
int temp = 0;
int count = 0;
for (int res = arr.length / 2; res > 0; res /= 2) {
for (int i = res; i < arr.length; i++) {
for (int j = i - res; j >= 0; j -= res) {
if (arr[j] > arr[j + res]) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
System.out.println("第" + (++count) + "轮为=" + Arrays.toString(arr));
}
}
public static void shellSort1(int[] arr) {
int count = 0;
for(int res=arr.length/2;res>0;res/=2) {
for(int i=res;i<arr.length;i++) {
int j=i;
int temp=arr[j];
if(arr[j]<arr[j-res]) {
while(j-res>=0 && temp<arr[j-res]) {
arr[j]=arr[j-res];
j-=res;
}
arr[j]=temp;
}
}
System.out.println("第" + (++count) + "轮为=" + Arrays.toString(arr));
}
}
}
归并排序 MergeSort
c++
分治思想
#include <iostream>
using namespace std;
void merge(int arr[], int left, int mid, int right, int temp[])
{
int i = left;
int j = mid + 1;
int t = 0;
while (i <= mid && j <= right)
{
if (arr[i] <= arr[j])
{
temp[t++] = arr[i++];
}
else
{
temp[t++] = arr[j++];
}
}
while (i <= mid)
{
temp[t++] = arr[i++];
}
while (j <= right)
{
temp[t++] = arr[j++];
}
t = 0;
int templeft = left;
while (templeft <= right)
{
arr[templeft++] = temp[t++];
}
}
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);
}
}
int main(int argc, char const *argv[])
{
int arr[] = {1, 5, 8, 23, 9, 2, 4, 66};
int len = sizeof(arr) / sizeof(arr[0]);
int *temp = new int[len];
mergeSort(arr, 0, len - 1, temp);
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
system("pause");
return 0;
}
快排 QuickSort
c++
#include <iostream>
using namespace std;
int getindex(int* arr, int low, int high)
{
int temp = arr[low];
while (low < high)
{
while (low < high && arr[high] > temp)
{
high--;
}
arr[low] = arr[high];
while (low < high && arr[low] < temp)
{
low++;
}
arr[high] = arr[low];
}
arr[low] = temp;
return low;
}
void quickSort(int* arr, int low, int high)
{
if (low < high)
{
int index = getindex(arr, low, high);
quickSort(arr, low, index - 1);
quickSort(arr, index + 1, high);
}
return;
}
int main(int argc, char const *argv[])
{
int arr[] = { 8, 2, 0, -1, 22};
int ind = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, ind-1);
for(int i=0;i<ind;i++)
{
printf("%d ",arr[i]);
}
system("pause");
return 0;
}
基数排序 radixSort
cpp
#include <iostream>
using namespace std;
void radixSort(int arr[],int len);
int main()
{
int arr[] = {1,5,4,6016,458,15,27};
int len = sizeof(arr)/sizeof(arr[0]);
radixSort(arr,len);
system("pause");
return 0;
}
void radixSort(int arr[],int len)
{
int max = arr[0];
int maxLength=0;
for(int i=1;i<len-1;i++)
{
if(max<arr[i]) max=arr[i];
}
while(max)
{
max/=10;
maxLength++;
}
int **bucket = new int*[10];
int *bucketCounts = new int[10]();
for(int i=0;i<10;i++)
{
bucket[i] = new int[len];
}
for(int i=0,n=1;i<maxLength;i++,n*=10)
{
int index =0;
for(int j=0;j<len;j++)
{
int dight = arr[j]/n%10;
bucket[dight][bucketCounts[dight]] =arr[j];
bucketCounts[dight]++;
}
for(int k=0;k<10;k++)
{
if(bucketCounts[k]!=0)
{
for(int l=0;l<bucketCounts[k];l++)
{
arr[index++]=bucket[k][l];
}
}
bucketCounts[k]=0;
}
printf("第%d轮arr[]=",i+1);
for(int i=0;i<index;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
}