八大排序算法 JAVA实现 亲自测试 可用!
今天很高兴 终于系统的实现了八大排序算法!不说了 直接上代码 !代码都是自己敲的, 亲测可用没有问题!
另:说一下什么是八大排序算法:
插入排序
希尔排序
选择排序
堆排序
冒泡排序
快速排序
归并排序
基数排序
public class SortHandler {
public static void main(String args[])
{
//int a[] = {13,12,5,71,2,54,9,6};
// straightInsertSort(a,8);
// print(a,8);
// shellSort(a, 8);
// print(a,8);
// simpleSelectSort(a,8);
// print(a,8);
// bubbleSort2(a, 8);
// print(a,8);
// quickSort(a,8);
// print(a,8);
// print(a,8);
// heapSort(a,8);
// print(a,8);
// print(a,8);
// mergeSort(a);
// print(a,8);
//
// int[] a = { 49, 38, 65, 197, 76, 213, 27, 50 };
// radixSort(a);
}
public static void print(int a[], int n){
for(int j= 0; j<n; j++){
System.out.print(a[j]);
System.out.print(" ");
}
System.out.println();
}
public static void straightInsertSort(int a[], int n)
{
if(n <= 1)
{
return;
}
for(int i = 1; i < n; i++)
{
int x = a[i];
int j = i -1;
while(j >= 0 && a[j] > x)
{
a[j+1] = a[j];
j--;
}
a[j+1] = x;
print(a,8);
}
}
public static void shellInsertSort(int a[], int n, int dk)
{
if(n <= 1 || dk > n)
{
return;
}
for(int i = dk * 2 -1; i < n; i++)
{
int x = a[i];
int j = i - dk;
while(j >= 0 && a[j] > x)
{
a[j+dk] = a[j];
j = j - dk;
}
a[j+dk] = x;
print(a,8);
}
}
public static void shellSort(int a[], int n)
{
int dk = n/2;
while(dk >= 1)
{
shellInsertSort(a, n, dk);
dk = dk/2;
}
}
public static void simpleSelectSort(int a[], int n)
{
int index = 0;
for(int i = 0; i < n-1; i++)
{
index = i;
for(int j = i; j < n-1; j++)
{
if(a[j+1] < a[index])
{
index = j+1;
}
}
if(index != i)
{
int temp = a[index];
a[index] = a[i];
a[i] = temp;
}
print(a,8);
}
}
public static void heapSort(int a[], int length)
{
buildHeap(a, length);
print(a,8);
for(int i = length -1; i > 0; i --)
{
swap(a, 0, i);
heapAjust(a, 0, i-1);
print(a,8);
}
}
public static void buildHeap(int a[], int length)
{
for(int i = length/2 -1; i >= 0; i --)
{
heapAjust(a, i, length);
}
}
public static void heapAjust(int a[], int s, int length)
{
if(s < 0)
{
return;
}
int child = 2 * s + 1;
while(child < length)
{
if(child + 1 < length && a[child] > a[child + 1])
{
child ++;
}
if(a[child] < a[s])
{
swap(a, child, s);
s = child;
child = 2*s + 1;
}
else
{
break;
}
}
}
public static void bubbleSort(int a[], int n)
{
for(int i = 0; i < n-1; i++)
{
for(int j = 0; j < n-i-1; j++)
{
if(a[j] > a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
print(a,8);
}
}
public static void bubbleSort2(int a[], int n)
{
int high, low, temp;
high = n -1;
low = 0;
while(low < high)
{
for(int j = low; j < high; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
for(int k = high; k > low; k--)
{
if(a[k] < a[k-1])
{
temp = a[k];
a[k] = a[k-1];
a[k-1] = temp;
}
}
low ++;
high --;
print(a, 8);
}
}
public static void swap(int a[], int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static int partition(int a[], int low, int high)
{
int pivotLoc = low;
while(low < high)
{
while(a[high] > a[pivotLoc] && low < high)
{
high --;
}
if(low < high)
{
swap(a, pivotLoc, high);
pivotLoc = high;
}
else
{
break;
}
while(a[low] < a[pivotLoc] && low < high)
{
low ++;
}
if(low < high)
{
swap(a, pivotLoc, low);
pivotLoc = low;
}
else
{
break;
}
}
print(a, 8);
return pivotLoc;
}
public static void quickSort(int a[], int low, int high)
{
if(low < high)
{
int pivotLoc = partition(a, low, high);
quickSort(a, low, pivotLoc - 1);
quickSort(a, pivotLoc + 1, high);
}
}
public static void quickSort(int a[], int length)
{
quickSort(a, 0, length-1);
}
public static void mergeSort(int a[])
{
int low = 0, high = a.length - 1;
mergeSort(a, low, high);
}
public static void mergeSort(int a[], int low, int high)
{
System.out.println("mergeSort-- " + "low: " + low + " high: " + high);
int mid = (low + high) / 2;
if(low < high)
{
mergeSort(a, low, mid);
mergeSort(a, mid + 1, high);
merge(a, low, mid, high);
}
print(a,8);
return;
}
private static void merge(int[] a, int low, int mid, int high)
{
if(low >= high)
{
return;
}
int[] temp = new int[high - low + 1];
int i=low, j=mid + 1, k=0;
for(; i <= mid && j <= high; k++)
{
if(a[i] < a[j])
{
temp[k] = a[i];
i++;
}
else
{
temp[k] = a[j];
j++;
}
}
if(i <= mid)
{
for(; i <= mid; i++,k++)
{
temp[k] = a[i];
}
}
else if(j <= high)
{
for(; j <= high; j++,k++)
{
temp[k] = a[j];
}
}
for(int m = 0; m < temp.length; m++)
{
a[m + low] = temp[m];
}
}
public static int getMaxDigits(int a[])
{
//first get the max one
int max = a[0];
for(int i = 1; i < a.length; i++)
{
if(max < a[i])
{
max = a[i];
}
}
if(max < 0)
{
return -1;
}
//then get the max digits
int digits = 0;
while(max > 0)
{
digits ++;
max /= 10;
}
return digits;
}
public static int getNumInPosition(int number, int digit)
{
for(int i = 0; i < digit; i ++)
{
number /= 10;
}
number %= 10;
return number;
}
public static void radixSort(int a[])
{
int length = a.length;
int digits = getMaxDigits(a);
int array[][] = new int[10][a.length + 1];
for(int i = 0; i < 10; i++)
{
//the first element stores the column count;
//instantiate
array[i][0] = 0;
}
print(a, 8);
for(int i = 0; i < digits; i++)
{
for(int j = 0; j < length; j++)
{
int row = getNumInPosition(a[j], i);
int col = ++array[row][0];
array[row][col] = a[j];
}
for(int row = 0, m = 0; row < 10; row++)
{
int cols = array[row][0];
for(int col = 1; col <= cols; col++)
{
a[m++] = array[row][col];
}
//reinitialize
array[row][0] = 0;
}
print(a, 8);
}
}
}
八大排序算法 JAVA实现 亲自测试 可用!的更多相关文章
- 八大排序算法Java实现
本文对常见的排序算法进行了总结. 常见排序算法如下: 直接插入排序 希尔排序 简单选择排序 堆排序 冒泡排序 快速排序 归并排序 基数排序 它们都属于内部排序,也就是只考虑数据量较小仅需要使用内存的排 ...
- 八大排序算法Java
目录(?)[-] 概述 插入排序直接插入排序Straight Insertion Sort 插入排序希尔排序Shells Sort 选择排序简单选择排序Simple Selection Sort 选择 ...
- 八大排序算法java代码
1.冒泡排序 public static void main(String[] args) { int[] arr = {1,4,2,9,5,7,6}; System.out.println(&quo ...
- 八大排序算法总结与java实现(转)
八大排序算法总结与Java实现 原文链接: 八大排序算法总结与java实现 - iTimeTraveler 概述 直接插入排序 希尔排序 简单选择排序 堆排序 冒泡排序 快速排序 归并排序 基数排序 ...
- 八大排序算法详解(动图演示 思路分析 实例代码java 复杂度分析 适用场景)
一.分类 1.内部排序和外部排序 内部排序:待排序记录存放在计算机随机存储器中(说简单点,就是内存)进行的排序过程. 外部排序:待排序记录的数量很大,以致于内存不能一次容纳全部记录,所以在排序过程中需 ...
- Java八大排序算法
Java八大排序算法: package sort; import java.util.ArrayList; import java.util.Arrays; import java.util.List ...
- [Data Structure & Algorithm] 八大排序算法
排序有内部排序和外部排序之分,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存.我们这里说的八大排序算法均为内部排序. 下图为排序 ...
- Python实现八大排序算法(转载)+ 桶排序(原创)
插入排序 核心思想 代码实现 希尔排序 核心思想 代码实现 冒泡排序 核心思想 代码实现 快速排序 核心思想 代码实现 直接选择排序 核心思想 代码实现 堆排序 核心思想 代码实现 归并排序 核心思想 ...
- Python - 八大排序算法
1.序言 本文使用Python实现了一些常用的排序方法.文章结构如下: 1.直接插入排序 2.希尔排序 3.冒泡排序 4.快速排序 5.简单选择排序 6.堆排序 7.归并排序 8.基数排序 上述所有的 ...
随机推荐
- clipboard.js小说明
github主页 clipboard.js是一个github上的开源项目,可以实现纯 JavaScript (无 Flash)的浏览器内容复制到系统剪贴板的功能. 用法 <script type ...
- Entity Framework加载数据的三种方式。
MSDN文章Loading Related Entities 有 Eagerly Loading Lazy Loading Explicitly Loading 三种方式. 而看到查询中包含Inclu ...
- uwp - 上滑隐藏导航栏下滑显示
原文:uwp - 上滑隐藏导航栏下滑显示 好久没写博客了,因为忙着工作.昨天周末填坑需要做一个上滑列表数据时隐藏导航栏下滑时显示的效果,下面分享一下我的做法,希望能给你带来帮助. 思路是通过判断滚动条 ...
- 在vs中启动项目,同时给项目传递参数
问题的引出:项目在startup.cs文件中做了控制,根据读取的控制台的ip 和端口启动项目 : dotnet project --ip 127.0.0.1 --port 8001 这样写的好处是 ...
- HDU2665 Kth number 【合并树】
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Java异常处理错误
Java异常处理错误 研究发现,在编译阶段的最佳时机错误,序之前.然而,编译期间并不能找出全部的错误,余下的问题必须在执行阶段解决.这就须要错误源通过某种方式把适当的信息传给某个接收者,该接收者知道怎 ...
- Sync Framework Toolkit 开源库
Sync Framework Toolkit构建在Sync Framework 2.1之上,使用OData在所有平台或客户端——包括Windows Phone 7.Silverlight.Window ...
- WPF Binding Path妙用
<Window x:Class="XamlTest.Window9" xmlns="http://schemas.microsoft.com/winf ...
- JPlayer-MP3播放器(带播放列表)
第一篇随笔,写的不好的地方,各位不要见笑.其他的也不多说了,下面是我在工作中用的一个基于JQuery开源的插件,官方地址:http://www.jplayer.org/.先看下要实现的效果图: 首先引 ...
- Keil c中自定义带可变参数的printf函数
在嵌入式c中,往往采用串口打印函数来实现程序的调试,而在正式程序中一般是不需要这些打印代码的,通常做法是在这些调试用打印代码的前后设置一个宏定义块来实现是否启用这段代码,比如: // other us ...