zoukankan      html  css  js  c++  java
  • 经典排序算法

    快排:

    static int partition(int[] unsorted, int low, int high)
            {
                int pivot = unsorted[low];
                while (low < high)
                {
                    while (low < high && unsorted[high] > pivot) high--;
                    unsorted[low] = unsorted[high];
                    while (low < high && unsorted[low] <= pivot) low++;
                    unsorted[high] = unsorted[low];
                }
                unsorted[low] = pivot;
                return low;
            }
    
            static void quick_sort(int[] unsorted, int low, int high)
            {
                int loc = 0;
                if (low < high)
                {
                    loc = partition(unsorted, low, high);
                    quick_sort(unsorted, low, loc - 1);
                    quick_sort(unsorted, loc + 1, high);
                }
            }
    

      桶排序:

    static int[] bucket_sort(int[] unsorted, int maxNumber = 99)
            {
                int[] sorted = new int[maxNumber + 1];
                for (int i = 0; i < unsorted.Length; i++)
                {
                    sorted[unsorted[i]] = unsorted[i];
                }
                return sorted;
            }
    

      冒泡:

      static void bubble_sort(int[] unsorted)
            {
                for (int i = 0; i < unsorted.Length; i++)
                {
                    for (int j = i; j < unsorted.Length; j++)
                    {
                        if (unsorted[i] > unsorted[j])
                        {
                            int temp = unsorted[i];
                            unsorted[i] = unsorted[j];
                            unsorted[j] = temp;
                        }
                    }
                }
            }
    

      选择排序:

    static void selection_sort(int[] unsorted)
            {
                for (int i = 0; i < unsorted.Length; i++)
                {
                    int min = unsorted[i], min_index = i;
                    for (int j = i; j < unsorted.Length; j++)
                    {
                        if (unsorted[j] < min)
                        {
                            min = unsorted[j];
                            min_index = j;
                        }
                    }
                    if (min_index != i)
                    {
                        int temp = unsorted[i];
                        unsorted[i] = unsorted[min_index];
                        unsorted[min_index] = temp;
                    }
                }
            }
    

      插入排序:

    static void insertion_sort(int[] unsorted)
            {
                for (int i = 1; i < unsorted.Length; i++)
                {
                    if (unsorted[i - 1] > unsorted[i])
                    {
                        int temp = unsorted[i];
                        int j = i;
                        while (j > 0 && unsorted[j - 1] > temp)
                        {
                            unsorted[j] = unsorted[j - 1];
                            j--;
                        }
                        unsorted[j] = temp;
                    }
                }
            }
    

      归并排序:

    1. //将有二个有序数列a[first...mid]和a[mid...last]合并。  
    2. void mergearray(int a[], int first, int mid, int last, int temp[])  
    3. {  
    4.     int i = first, j = mid + 1;  
    5.     int m = mid,   n = last;  
    6.     int k = 0;  
    7.       
    8.     while (i <= m && j <= n)  
    9.     {  
    10.         if (a[i] <= a[j])  
    11.             temp[k++] = a[i++];  
    12.         else  
    13.             temp[k++] = a[j++];  
    14.     }  
    15.       
    16.     while (i <= m)  
    17.         temp[k++] = a[i++];  
    18.       
    19.     while (j <= n)  
    20.         temp[k++] = a[j++];  
    21.       
    22.     for (i = 0; i < k; i++)  
    23.         a[first + i] = temp[i];  
    24. }  
    25. void mergesort(int a[], int first, int last, int temp[])  
    26. {  
    27.     if (first < last)  
    28.     {  
    29.         int mid = (first + last) / 2;  
    30.         mergesort(a, first, mid, temp);    //左边有序  
    31.         mergesort(a, mid + 1, last, temp); //右边有序  
    32.         mergearray(a, first, mid, last, temp); //再将二个有序数列合并  
    33.     }  
    34. }  
    35.   

      

  • 相关阅读:
    npm tip: go to the package's home page
    centos7在Evolution中配置163邮箱,被阻止收件解决方法
    emacs-显示行号以及跳转到指定行
    2020年学习目标之一——emacs
    学习前端的时候,突然想起了Sharepoint母版页里的占位符,算知识的融会不?
    问题记录--jekyll serve 启动的时候如何指定80端口
    为什么总是无法访问VMware内的web服务?
    python开发目录合并小工具 PathMerge
    python计算文件的md5值
    python+selenium 简单尝试
  • 原文地址:https://www.cnblogs.com/gaoxiangde/p/4341024.html
Copyright © 2011-2022 走看看