Java数据结构和算法--排序

[color=red]差别: 冒泡要不停的交换,选择是碰到最小的,和最前面的索引位置的进行交换。
排序的方法有:插入排序(直接插入排序、希尔排序),交换排序(冒泡排序、快速排序),选择排序(直接选择排序、堆排序),归并排序,分配排序(箱排序、基数排序)[/color]

1.冒泡排序

package ChapterOne;

public class Bubble {

public static void main(String[] args) {
Bubble b = new Bubble();
for(int i = 0;i < arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
b.bubble();
for(int i = 0;i < arr.length;i++){
System.out.print(arr[i]+" ");
}
System.exit(0);
}
/**
* 冒泡排序
*/
public void bubble(){
for(int i = arr.length-1;i > 1;i--){
for(int j = 0;j < i;j++){
if(arr[j] > arr[j+1])
swap(j,j+1);
}
}
}
/**
* 交换数组中的两个数
* @param one
* @param two
*/
public void swap(int one,int two){
long temp = arr[one];
arr[one] = arr[two];
arr[two] = temp;
}

static long arr[] = new long[20];
/**
* 随机初始化一个长度为20的数组
*/
static{
for(int i = 0;i < arr.length;i++){
arr[i] = (long) (Math.random()*100);
}
}
}

2.选择排序

package ChapterOne;

public class SelectSort {
/**
* 初始化一个长度为size的数组
* @param size
*/
public SelectSort(int size){
arr = new long[size];
for(int i = 0;i < size;i++){
arr[i] = (long) (Math.random()*100);
}
}
/**
* 选择排序
* @return
*/
public long[] sort(){
for(int i = 0;i < arr.length;i++){
int minIndex = findMin(i);
long min = arr[minIndex];
//子数组中的所有的数后移一个位置
for(int j = minIndex;j > 0;j--)
arr[j] = arr[j-1];
//把最小的数插入到最前端
arr[0] = min;
}
return arr;
}
/**
* 查找一start为起始位置到数组最后位置这段子数组中最小的一个数的位置
* @param start
* @return
*/
public int findMin(int start){
int min = start;
for(int i = min;i < arr.length;i++)
if(arr[min] > arr[i])
min = i;
return min;
}

public static void main(String[] args) {
SelectSort ss = new SelectSort(10);
ss.print();
long oa[] = ss.sort();
for(int i = 0;i < oa.length;i++)
System.out.print(oa[i]+" ");
System.out.println();
System.exit(0);
}
public void print(){
for(int i = 0;i < arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}

long arr[];
}


3.快速排序


void paixu(int a[],int low,int high;)//用快速排序法
...{
  // low, high表示扫描的范围

  int pivot;//存放中心索引及其值的局部变量
  int scanup,scandown,mid;//用于扫描的索引
  if (high-low<=0) //如果数组中的元素少于两个,则返回
   return;
  else
   if(high-low==1) //如果有两个元素,对其进行比较
   ...{
    if(apai[high]<apai[low]) //如果后一个比前一个小,
     Swap(apai[low],apai[high]);//那么交换位置
     return;
   }//end if
  mid=(low+high)/2;//取得中心索引
  pivot=apai[mid];//将中间索引的值,赋给pivot
  Swap(apai[mid],apai[low]);//交换pivot及低端元素的值
  Scanup=low+1;
  Scandown=high;//初始化扫描索引scanup和scandown
  do...{
   //从低端子表向上扫描,当scanup进入高端子表或遇到大于pivot的元素时结束.
   while(scanup<=scandown && apai[scanup]<=pivot)
    scanup++;
    //从高端子表向下扫描,当scandown遇到小于或等于pivot的元素时结束
   while(piovt<apai[scandown])
    scandown--;
    //如果两个索引还在各自的子表中,则表示两个元素错位,将两个元素换位
    if(scanup<scandown)
     Swap(apai[scanup],apai[scandown]);
  }while(scanup<scandown);
  //将pivot拷贝到scandown位置,分开两个子表
  apai[low]=apai[scandown];
   apai[scandown]=pivot;
  //如果低端子表(low至scandown-1)有2个或更多个元素,则进行递归调用
  if(low<scandown-1)
   paixu(apai,low,scandown-1);
   //如果高端子表(scandown+1至high) 有2个或更多个元素,则进行递归调用
  if(scandown+1<high)
   paixu(apai, scandown+1, high);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值