• 排序算法源码(JAVA)


     
    public class Test {

    public static void main(String[] args){
    Sort sort = new Sort();
    sort.sortTest();
    }

    }
    
    
    public class Sort {

    public void sortTest() {

    int[] r = new int[]{9,8,7,5,6,4,3,2,1};
    int[] r1 = new int[]{-1,9,8,7,5,6,4,3,2,1};

    insertSortTest(r);
    shellSortTest(r);
    bubbleSortTest(r);
    quickSortTest(r);
    selectSortTest(r);
    heapSortTest(r1);
    mergeSortTest(r1);


    ElemNode[] elemNodes = new ElemNode[10];
    for(int i = 0; i < elemNodes.length; i++){
    elemNodes[i] = new ElemNode();
    elemNodes[i].key = 9 - i;
    elemNodes[i].next = 0;
    }
    KeyNode[] keyNodes = new KeyNode[10]; //key为0-9
    for(int j = 0;j < keyNodes.length; j++){
    keyNodes[j] = new KeyNode();
    }
    bucketSortTest(elemNodes,keyNodes);

    }

    public void insertSortTest(int[] r){
    insertSort(r);

    System.out.print("Insertion Sort : ");
    for(int i = 0; i < r.length; i++){
    System.out.print( r[i] + " ");
    }
    System.out.println();
    }

    public void shellSortTest(int[] r){
    shellSort(r);

    System.out.print("Shell Sort : ");
    for(int i = 0; i < r.length; i++){
    System.out.print( r[i] + " ");
    }
    System.out.println();
    }

    public void bubbleSortTest(int[] r){
    bubbleSort(r);

    System.out.print("Bubble Sort : ");
    for(int i = 0; i < r.length; i++){
    System.out.print(r[i] + " ");
    }
    System.out.println();
    }

    public void quickSortTest(int[] r){
    quickSort(r, 0, r.length - 1);

    System.out.print("Quicksort : ");
    for(int i = 0; i < r.length; i++){
    System.out.print(r[i] + " ");
    }
    System.out.println();
    }

    public void selectSortTest(int[] r){
    selectSort(r);

    System.out.print("Selection Sort : ");
    for(int i = 0; i < r.length; i++){
    System.out.print( r[i] + " ");
    }
    System.out.println();
    }

    //下标从1开始
    public void heapSortTest(int[] r){
    heapSort(r, r.length - 1); //下标从1开始

    System.out.print("Heap Sort : ");
    for(int i = 0; i < r.length; i++){
    System.out.print( r[i] + " ");
    }
    System.out.println();
    }

    //下标从1开始
    public void mergeSortTest(int[] r){
    int[] r1 = new int[r.length];
    mergeSort(r, r1, r.length - 1); //下标从1开始

    System.out.print("Merge Sort : ");
    for(int i = 0; i < r.length; i++){
    System.out.print(r[i] + " ");
    }
    System.out.println();
    }

    public void bucketSortTest(ElemNode[] r, KeyNode[] q){
    int index = bucketSort(r, r.length, q, q.length);

    System.out.print("Bucket Sort : ");

    for(int i = 0; i < r.length; i++){
    System.out.print(r[index].key + " ");
    index = r[index].next;
    }
    System.out.println();
    }


    //////////////////////////////////////////具体实现////////////////////////////////////////////

    //////////////////////////////////////////直接插入排序////////////////////////////////////////////

    public void insertSort(int[] r){

    int temp, i ,j;

    for( i = 1; i < r.length; i++){
    temp = r[i];
    for(j = i - 1; j >= 0 && temp < r [j]; j-- )
    r[j+1] = r[j];
    r[j+1] = temp;
    }
    }

    //////////////////////////////////////////希尔实现////////////////////////////////////////////

    public void shellSort(int[] r){

    int temp, d, i , j;

    for(d = r.length/2; d >= 1; d = d/2){
    for(i = d ; i < r.length; i++){
    temp = r[i];
    for(j = i - d; j >= 0 && temp < r[j]; j = j-d)
    r[j+d] = r[j];
    r[j+d] = temp;
    }
    }
    }

    //////////////////////////////////////////冒泡排序////////////////////////////////////////////

    public void bubbleSort(int[] r){

    int exchange = r.length - 1;//动态记录过程
    int bound = r.length - 1;//冒泡边界初始化

    while (exchange != -1){
    bound = exchange;//冒泡排序边界
    exchange = -1; //交换过程记录

    for(int i = 0; i < bound; i++){
    if (r[i] > r[i+1]){
    int temp = r[i];
    r[i] = r[i+1];
    r[i+1] = temp;

    exchange = i;
    }
    }
    }

    }


    //////////////////////////////////////////快速排序////////////////////////////////////////////

    public int partition(int[] r, int first, int end){

    int i = first, j = end ,temp;

    while (i < j){
    while (i < j && r[i] < r[j]) j--;
    if (i < j ) {
    temp = r[i];
    r[i] = r[j];
    r[j] = temp;

    i++;
    }

    while (i < j && r[i] < r[j]) i++;
    if(i < j){
    temp = r[i];
    r[i] = r[j];
    r[j] = temp;

    j--;
    }
    }
    return i;
    }

    public void quickSort(int[] r, int first, int end){
    if (first < end){
    int p = partition(r,first, end);
    quickSort(r,first,p - 1);
    quickSort(r,p + 1,end);
    }
    }

    //////////////////////////////////////////选择排序////////////////////////////////////////////

    public void selectSort(int[] r){

    int index;//记录临时最小值序号

    for(int i = 0;i < r.length - 1; i++){

    index = i; //初始化最小序号为当前位置序号

    for(int j = i + 1; j < r.length; j++)//从当前序号后(无序区)选最小值
    if(r[j] < r[index]) index = j;

    if (index != i){ //根据最小值下标与当前位置交换
    int temp = r[index];
    r[index] = r[i];
    r[i] = temp;
    }
    }
    }

    //////////////////////////////////////////堆排序////////////////////////////////////////////

    //堆存储结构:元素在数组中按层序遍历顺序排序(下标从1开始)

    //(子堆已经建好后)调整堆顶
    public void sift(int[] r, int k, int m){ //从小到大排:建立最大堆
    //k:堆顶序号 m:最后节点序号
    int i = k , j = 2 * i; //i:堆顶序号 j:堆顶孩子节点序号
    while(j <= m){
    if(j < m && r[j] < r[j+1]) j++; //j为左右子节点中较大的

    if(r[i] < r[j]){ //交换父子节点
    int temp = r[i];
    r[i] = r[j];
    r[j] = temp;

    i = j; j = 2 * i; //调整改变堆顶后的子堆
    }
    else break;
    }
    }

    //堆排序:由下至上建立堆,堆顶元素交换到数组尾部
    public void heapSort(int[] r, int n){

    for(int i = n/2; i >= 1; i--){ //由下至上建立堆
    sift(r,i,n);
    }

    for(int i = 1; i < n; i++){ //i:第i次取顶

    //堆顶元素交换到数组尾部(排序)
    int temp = r[1];
    r[1] = r[n - (i - 1)];
    r[n - (i - 1)] = temp;

    sift(r,1,n - i);//对堆顶重新建堆(未排序)
    }

    }



    //////////////////////////////////////////归并排序////////////////////////////////////////////

    public void merge(int[] r, int[] r1, int s, int m, int t)//一节
    {
    int i = s, j = m + 1; //r下标
    int k = s; //r1下标

    while(i <= m && j <= t) //取两子序列r[i] r[j]中较小者放入r1
    {
    if(r[i] < r[j]) r1[k++] = r[i++];
    else r1[k++] = r[j++];
    }

    //收尾处理
    if(i <= m)
    while(i <= m) r1[k++] = r[i++];
    else
    while(j <= t) r1[k++] = r[j++];

    }

    public void mergePass(int r[], int r1[], int n, int h)//一趟(总长n,子序列长h)
    {
    int i = 1; //下标从1开始
    while(i < n - 2*h + 1) //归并{h, h}
    {
    merge(r, r1, i, i+h-1, i+2*h-1);
    i = i + 2*h;
    }
    if(i < n - h + 1) //归并{h, <h}
    merge(r, r1, i, i+h-1, n);
    else //归并 <h
    for(int k = i; k <= n ;k++) r1[k] = r[k];
    }

    public void mergeSort(int r[], int r1[], int n)
    {
    int h = 1;
    while(h < n){
    mergePass(r, r1, n, h); //r -> r1
    h = 2*h;
    mergePass(r1, r, n, h); //r1 -> r
    h = 2*h;
    }
    }

    //////////////////////////////////////////桶排序////////////////////////////////////////////

    public class ElemNode //静态链表
    {
    ElemNode(){}
    public int key; //排序索引
    // int value; //具体值
    public int next; //静态链表的next index
    }

    public class KeyNode //key桶
    {
    public int front; //桶首的静态链表index
    public int rear; //桶尾的静态链表index
    }

    //分配
    public void distribute(ElemNode[] r, int n, KeyNode[] q, int m)
    {
    int i = 0 ; //静态链表从0开始

    while(i < n) //扫描静态链表
    {
    int k = r[i].key;
    if(q[k].front == -1) q[k].front = i; //key桶为空:设置key桶头部
    else r[q[k].rear].next = i; //key桶非空:静态链表连接桶元素

    q[k].rear = i; //设置key桶尾部

    i++; //i后移,处理静态链表下一个元素
    }
    }

    //收集
    public int collect(ElemNode[] r, int n, KeyNode[] q, int m)
    {
    int k = 0; //key桶从0开始
    while(q[k].front == -1) k++; //找到第一个非空key桶

    int first = q[k].front; //静态链表首部index
    int last = q[k].rear; //静态链表尾部index

    while (k < m) //扫描key桶
    {
    k++;
    if (k < m && q[k].front != -1) //找到下一个非空key桶
    {
    r[last].next = q[k].front; //静态链表连接
    last = q[k].rear; //修改静态链表尾部index
    }
    }
    r[last].next = -1; //设置静态链表排序后尾部标志

    return first; //返回排序后的静态链表首部index
    }

    public int bucketSort(ElemNode[] r, int n, KeyNode[] q, int m)
    {
    //初始化静态链表
    for(int i = 0; i < n; i++) r[i].next = i + 1;
    r[n - 1].next = -1; //尾标志为-1

    //初始化key桶
    for(int j = 0; j < m; j++){
    q[j].front = -1;
    q[j].rear = -1;
    }

    distribute(r,n,q,m);
    return collect(r,n,q,m);
    }

    }
     
     
     
     
     





  • 相关阅读:
    Vue生命周期
    事件
    改变this指向
    全屏要素
    微信小程序
    编程心得
    设计模式-单例与实例化
    第五节课 课堂总结
    作业四 分支结构
    课堂总结
  • 原文地址:https://www.cnblogs.com/Doing-what-I-love/p/5535096.html
Copyright © 2020-2023  润新知