java常用数据结构排序算法
来源:互联网 发布:矩阵的伴随矩阵 编辑:程序博客网 时间:2024/06/05 20:54
import java.util.*;
public class TestSort1{
private int[] a = {9,5,8,2,1,4,3,7};
private int n = 8;
public void BubbleSortedArray(){ //冒泡排序
for(int i = 1;i<n;i++){ //n-1次通道
for(int j = 0;j<n-i; j++){//比较n-i次
if(a[j] > a[j+1]){
int temp;
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
for(int k = 0;k<n;k++){
System.out.print(a[k]+" ");
}
System.out.println();
}
}
public void selectSort(){ //选择排序
for(int i = 0;i<n-1;i++){ //n-1个通道
int k = i; //记录位置
for(int j=i+1;j<n;j++){
if(a[j]<a[k]){
k = j;
}
}
int temp;
temp = a[i];
a[i] = a[k];
a[k] = temp;
for(int m = 0;m<n;m++){
System.out.print(a[m]+" ");
}
System.out.println();
}
}
public void insertSort(){ //插入排序
for(int i = 1; i<=n-1; i++){//未排序数据
int temp = a[i];
int j= i-1;
if(a[i]<a[i-1]){a[i] = a[i-1];
for(;j>=0 && temp<a[j];--j){
a[j+1] = a[j];
}
}
a[j+1] = temp;
for(int m = 0;m<n;m++){
System.out.print(a[m]+" ");
}
System.out.println();
}
}
public void shellSort(){//shell排序
for(int increment = n/2;increment>0;increment/=2){//增量
for(int i = increment;i<n;++i){
int temp = a[i];
int j= i;
for(;j>= increment;j -=increment){ //对一组增量为increment的元素进行插入排序
if(temp < a[j-increment]){
a[j] = a[j-increment];
}else{
break;
}
}
a[j] = temp;
}
for(int m = 0;m<n;m++){
System.out.print(a[m]+" ");
}
System.out.println();
}
}
public void display(){
System.out.println();
System.out.println("---------------------------");
System.out.println(" 排好序的数据: ");
System.out.println("---------------------------");
for(int i = 0;i<n;i++){
System.out.print(a[i]+" ");
}
}
public static void main(String[] args){
TestSort1 ts = new TestSort1();
//ts.BubbleSortedArray();
//ts.selectSort();
//ts.insertSort();
ts.shellSort();
ts.display();
}
}
- java常用数据结构排序算法
- java常用数据结构排序算法(续)
- 常用数据结构和排序算法
- 数据结构中常用排序算法
- 数据结构常用排序算法总结
- 【数据结构】-常用排序算法对比
- 数据结构常用内部排序算法
- 数据结构之常用排序算法
- 【数据结构和算法】Java常用排序算法/程序员必须掌握的8大排序算法
- 【数据结构】数据结构中常用的排序算法
- 常用数据结构与算法之排序算法
- java常用排序算法
- Java 常用排序算法
- java常用排序算法
- java常用排序算法
- java 常用排序算法
- 常用Java排序算法
- Java常用排序算法
- Java谜题8——更多的库谜题
- 安装ASP.NET AJAX必看
- The file has not been pre-compiled错误
- 与AutoHotKey类似的轻量级自动化测试工具 - AutoIt
- 《Practices of an Agile Developer》读书笔记
- java常用数据结构排序算法
- [算法优化]优化阶乘算法的探索
- 评侯捷的<深入浅出MFC>和李久进的<MFC深入浅出>
- 再读uclinux-2008r1-rc8(bf561)内核的中断处理(1):数据结构
- C++程序设计之四书五经(下篇)
- May 28 2008
- CSS控制背景图片
- CSS控制背景图片
- 需求分析 (from zh_wikipedia)