关于八大排序的算法思想我这里不再赘述,直接上代码。存在错误之处望指正,大家一起进步!
----------------------------------------------------------------------------------------------------------------------
一、直接插入(插入排序)
/**
* 直接插入(插入排序)
* */
public class Insert_Sort_01 {
public static void InsertSort( int [] array){
for ( int i= 1 ; i<array. length ; i++){
if (array[i] < array[i- 1 ]){
int temp = array[i],j;
for (j=i- 1 ; j>- 1 && temp <array[j]; j--){
array[j+ 1 ] = array[j];
}
array[j+ 1 ] = temp;
}
}
}
public static void main(String[] args) {
int [] a = { 3 , 2 , 5 , 8 , 4 , 7 , 6 , 9 };
InsertSort (a);
for ( int i: a){
System. out .print( " " +i);
}
}
}
----------------------------------------------------------------------------------------------------------------------
二、希尔排序(插入排序)
/**
* 希尔排序(插入排序)
* */
public class Shell_Sort_02 {
public static void ShellSort( int [] array){
int n = array. length ;
int h;
for (h = n/ 2 ; h> 0 ; h/= 2 ){
for ( int i = h; i<n; i++){
for ( int j = i-h; j>= 0 ; j-=h){
if ( array[j] > array[j+h] ){
int temp = array[j];
array[j] = array[j+h];
array[j+h] = temp;
}
}
}
}
}
public static void main(String[] args) {
int [] a = { 3 , 2 , 5 , 8 , 4 , 7 , 6 , 9 };
ShellSort (a);
for ( int i:a){
System. out .print(i+ " " );
}
}
}
----------------------------------------------------------------------------------------------------------------------
三、直接选择(选择排序)
/**
* 直接选择(选择排序)
* */
public class Select_Sort_03 {
public static void SelectSort( int [] array) {
int n = array. length ;
for ( int i = 0 ; i < n; i++) {
int minIndex = i;
for ( int j = i + 1 ; j < n; j++) {
if (array[minIndex] > array[j]) {
minIndex = j;
}
}
if (i != minIndex) {
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
}
public static void main(String[] args) {
int [] a = { 3 , 2 , 5 , 8 , 4 , 7 , 6 , 9 };
SelectSort (a);
for ( int i:a){
System. out .print(i+ " " );
}
}
}
----------------------------------------------------------------------------------------------------------------------
四、堆排序(选择排序)
/**
* 堆排序(选择排序)
* */
public class Heap_Sort_04 {
public static void HeapSort( int [] array) {
// 创建最大堆:从最后一个节点的父节点开始
int lastIndex = array. length - 1 ;
int startIndex = (lastIndex - 1 ) / 2 ;
for ( int i = startIndex; i >= 0 ; i--) {
maxHeap ( array, array. length , i );
}
// 排序:末尾与头交换,逐一找出最大值,最终形成一个递增的有序序列
for ( int i = array. length - 1 ; i > 0 ; i--) {
int temp = array[ 0 ];
array[ 0 ] = array[i];
array[i] = temp;
maxHeap (array, i, 0 );
}
}
private static void maxHeap( int [] data, int heapSize, int index) {
// 左子节点
int leftChild = 2 * index + 1 ;
// 右子节点
int rightChild = 2 * index + 2 ;
// 最大元素下标
int largestIndex = index;
// 分别比较当前节点和左右子节点,找出最大值
if ( leftChild < heapSize && data[leftChild] > data[largestIndex] ) {
largestIndex = leftChild;
}
if ( rightChild < heapSize && data[rightChild] > data[largestIndex] ) {
largestIndex = rightChild;
}
// 如果最大值是子节点,则进行交换
if ( largestIndex != index ) {
int temp = data[index];
data[index] = data[largestIndex];
data[largestIndex] = temp;
// 交换后,其子节点可能就不是最大堆了,需要对交换的子节点重新调整
maxHeap (data, heapSize, largestIndex);
}
}
public static void main(String[] args) {
int [] array = { 3 , 2 , 5 , 8 , 4 , 7 , 6 , 9 };
HeapSort (array);
for ( int i:array){
System. out .print(i+ " " );
}
}
}
----------------------------------------------------------------------------------------------------------------------
五、冒泡排序(交换排序)
/**
* 冒泡排序(交换排序)
* */
public class Bubble_Sort_05 {
public static void BubbleSort_01( int [] array) {
int len = array. length ;
for ( int i = 0 ; i < len; i++) {
for ( int j = 0 ; j < len - i - 1 ; j++) {
if (array[j] > array[j + 1 ]) {
int temp = array[j + 1 ];
array[j + 1 ] = array[j];
array[j] = temp;
}
}
}
}
public static void BubbleSort_02( int [] array){
int len = array. length ;
boolean flag = true ;
while (flag){
flag = false ;
for ( int i = 0 ; i<len- 1 ; i++){
if (array[i] > array[i+ 1 ]){
int temp = array[i+ 1 ];
array[i+ 1 ] = array[i];
array[i] = temp;
flag = true ;
}
}
len--;
}
}
public static void main(String[] args) {
int [] array = { 3 , 2 , 5 , 8 , 4 , 7 , 6 , 9 };
BubbleSort_02 (array);
for ( int i:array){
System. out .print(i+ " " );
}
}
}
----------------------------------------------------------------------------------------------------------------------
六、快速排序(交换排序)
/**
* 快速排序(交换排序)
* */
public class Quick_Sort_06 {
public static void QuickSort( int [] array, int low, int high){
int t,i,j;
if ( high-low> 1 )
{
i=low+ 1 ;
j=high- 1 ;
while ( i<=j )
{
while ( i<high && array[i]<array[low] )i++;
while ( j>low && array[j]>=array[low] )j--;
if ( i<j )
{
t=array[i];
array[i]=array[j];
array[j]=t;
}
}
if ( (j-low)!= 0 )
{
t=array[low];
array[low]=array[j];
array[j]=t;
QuickSort (array,low,j);
QuickSort (array,j+ 1 ,high);
}
}
}
public static void main(String[] args) {
int [] array = { 3 , 2 , 5 , 8 , 4 , 7 , 6 , 9 };
QuickSort (array, 0 ,array. length );
for ( int i :array){
System. out .print(i+ " " );
}
}
}
----------------------------------------------------------------------------------------------------------------------
七、归并排序
/**
*归并排序
* */
public class Merge_Sort_07 {
public static void MergeSort( int [] a, int low, int high){
int mid,i,j,k;
int [] c = new int [ 100 ];
if (high-low> 1 )
{
mid=(low+high)/ 2 ;
MergeSort (a,low,mid);
MergeSort (a,mid,high);
k=i=low;j=mid;
while (i<mid||j<high)
{
if (j>=high||i<mid&&a[i]<a[j])
c[k++]=a[i++];
else
c[k++]=a[j++];
}
for (k=low;k<high;k++)
a[k]=c[k];
}
}
public static void main(String[] args){
int N = 9 ;
int i;
int [] a={ 3 , 2 , 5 , 8 , 4 , 7 , 6 , 9 , 1 };
MergeSort (a, 0 ,N);
for (i= 0 ;i<N;i++)
System. out .print( " " +a[i]);
}
}
----------------------------------------------------------------------------------------------------------------------
八、基数排序
这里要说明一下的是基数排序,负数和正数应该分开处理。这里我的代码优化的不是很好,后面会持续更新!
/**
* 基数排序
* @param //array 待排序数组
* @param //d 表示最大的元素的位数
* */
public class Radix_Sort_08 {
public static void RadixSort( int [] array, int d){
int n = 1 ;
int times = 1 ; // 排序次数,由位数最多的元素决定
int [][] temp = new int [ 10 ][array. length ]; //数组的第一维表示可能的余数0-9
int [] order = new int [ 10 ]; //数组order用来表示该位是i的元素个数
while (times <= d) {
for ( int i = 0 ; i < array. length ; i++) {
int lsd = ((array[i] / n) % 10 );
temp[lsd][order[lsd]] = array[i];
order[lsd]++;
}
int k = 0 ;
for ( int i = 0 ; i < 10 ; i++) {
if (order[i] != 0 ) {
for ( int j = 0 ; j < order[i]; j++) {
array[k] = temp[i][j];
k++;
}
order[i] = 0 ;
}
}
n *= 10 ;
times++;
}
}
public static int Maximum( int [] array){ //计算出数组中最大元素的位数
int max = array[ 0 ];
for ( int i= 0 ; i<array. length ; i++){
if ( array[i]>max )
max = array[i];
}
int digit = 0 ;
while ( max> 0 ){
max/= 10 ;
digit++;
}
return digit;
}
public static void Group( int [] array){
int negative_num = 0 ;
for ( int i= 0 ; i<array. length ; i++){
if ( array[i]< 0 )
negative_num++;
}
int [] negative = new int [negative_num] ;
int [] positive = new int [array. length -negative_num];
int n = 0 , p = 0 ;
for ( int i= 0 ; i<array. length ; i++){
if ( array[i]< 0 )
negative[n++] = array[i]*(- 1 );
else positive[p++] = array[i];
}
if (negative. length > 0 ){
RadixSort ( negative, Maximum (negative));
for ( int i = negative. length - 1 ; i>- 1 ; i--){
System. out .print(negative[i]*(- 1 )+ " " );
}
}
if (positive. length > 0 ){
RadixSort ( positive, Maximum (positive));
for ( int i:positive){
System. out .print(i+ " " );
}
}
}
public static void main(String[] args) {
int [] array = {- 3 ,- 2 , 5 ,- 8 ,- 0 , 0 , 6 , 9 };
Group (array);
}
}
八大排序(Java完整版)相关推荐
- 八大排序:Java实现八大排序及算法复杂度分析
目录 QUESTION:八大排序:Java实现八大排序及算法复杂度分析 ANSWER: 一:冒泡排序 1.算法分析 2.时间复杂度分析 3.代码 二:选择排序 1.算法分析 2.时间复杂度分析 3.代 ...
- 八大排序(JAVA)
文章目录 直接插入排序 希尔排序 选择排序 形式一: 形式二: 堆排序 冒泡排序 快速排序 Hoare 时间复杂度分析 注意点: 快排优化 挖坑法 前后指针法 三数取中法 递归到小区间 递归版本的优化 ...
- 八大排序Java代码(新)
八大排序 冒泡排序(稳定) 选择排序(不稳) 插入排序(稳定) 希尔排序(不稳) 快速排序(不稳) 归并排序(稳定) 基数排序(稳定) 堆排序(不稳) 冒泡排序(稳定) public class Bu ...
- 微信企业付款到银行卡(微信转账)(Java完整版)
业务介绍 微信付款到银行卡,也就是转账.需要将银行卡号.卡主真实姓名按照微信支付制定的加密规则进行加密,(开发完感觉加密比转账复杂,哈哈哈).加密的秘钥,也就是钥匙,也要通过请求向微信获取,重点是获取 ...
- 微信小程序码的生成(JAVA完整版) 亲测可用
JAVA生成小程序码(太阳码) 首先准备工具类,这里我使用的是QrUtil;废话不多说,上工具类; 工具类是获取token使用; appid = 小程序appID secret = 小程序秘钥 /** ...
- JAVA完整版WMS仓库管理系统PC+微信小程序端源码
WMS系统使用框架:springboot mybatis + redis +mysql + VUE + uniapp 包含:服务端JAVA全套开源源码, VUE后台前端开源代码+uniapp前端微信小 ...
- java完整版记事本_求java记事本完整版
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 p1.add(new Label("请选择大小")); p1.add(size); p2.add(new Label("请选 ...
- 2018最新廖雪峰全套Java完整版
# -*- coding: utf-8 -*- """ Created on Sat Nov 17 08:40:21 2018 @author: shenfangyuan ...
- 2019最新最全动脑学院数据结构与算法系列java完整版
本套据结构与算法视频很完整,有兴趣的朋友可以下载! 下载地址:百度网盘
最新文章
- Jupyter Notebook学习笔记
- 最长回文子串--马拉车(?)
- java 最大线程数 设定_Java8 parallelStream 修改默认的线程数量
- AlertBox 弹出层(信息提示框)效果
- Cookie 和 Session的区别 1
- Unity 2017 Game Optimization 读书笔记(4)Scripting Strategies Part 4
- c++ doxygen 注释规范_利用Doxygen给C程序生成注释文档
- Spring Security记住我功能之潜在的账号盗取风险
- 【讨论帖】你认为怎么注释是比较合理妥当的方式
- RAC中参数文件的配置
- python动态变量名_python实现可变变量名方法详解
- Dom4j完整教程,操作XML教程
- c语言C的ascii码是多少,c的ascii码值是多少
- Python轻松多条件计数与求和
- Lipschitz条件
- Pycharm安装打包工具
- 变量定义和声明的区别(整理)
- 《少年编程反汇编逆向调试入门》录制成功
- DV型、OV型、EV型三种SSL证书有什么区别?
- ip组播,IGMP协议,PIM协议
热门文章
- 工具和网络游戏封包基础
- 模拟退火算法的通俗理解及代码实现
- 浪潮服务器功耗计算器
- python科学计算库numpy和绘图库PIL的结合,素描图片(原创)
- 上善若水,绿之韵对传销说不!
- IT时代周刊封面文章:百度搜索公正性彻底调查
- vue使用Element的xlsx模板下载功能
- Java 进程的退出机制与Shutdown hook
- 图形学 实验四 梁barsky算法
- FLEXSIM资料下载