asp.net中C++插入排序 折半插入排序法例子

來源:互聯網
上載者:User

C++插入排序 包括折半插入排序法代碼,在直接插入排序,數組data用於存放待排序元素,n為待排序元素個數,同時還包括了對data數組中的元素進行希爾排序,n為該數組大小等:

 

 代碼如下 複製代碼
#include <iostream>
using namespace std;
#include "sort.h"
// 直接插入排序,數組data用於存放待排序元素,n為待排序元素個數
template<class ElemType>
void InsertSort(ElemType data[], int n)
{
    ElemType tmp;
 int i, j;
    for (i = 1; i < n; i++){
        if ( data[i] > data[i - 1])
            continue;
        tmp = data[i];                                // 儲存待插入的元素
  data[i] = data[i - 1];
        for ( j = i - 1; j > 0 && data[j - 1] > tmp;j--)
            data[j] = data[j - 1];                    // 元素後移
        data[j] = tmp;                                // 插入到正確位置       
    }
}
// 折半插入排序
template<class ElemType>
void BInsertSort(ElemType data[], int n)
{
    ElemType tmp;
 int i, j, mid, low, high;
    for (i = 1; i < n; i++){
        tmp = data[i]; // 儲存待插入的元素
        low = 0;
        high = i-1;
        while (low <= high){  // 在data[low..high]中折半尋找有序插入的位置
            mid = (low + high) / 2; // 折半
            if( tmp < data[mid])
                high = --mid; // 插入點在低半區
            else
                low = ++mid; // 插入點在高半區
        }
        for(j = i - 1; j >= low; j--)
            data[j + 1] = data[j]; // 元素後移
        data[low] = tmp;  // 插入到正確位置
    }
}
// 對data數組中的元素進行希爾排序,n為該數組大小
// increments為增量序列,incrementsLength為增量序列的大小
template<class ElemType>
void ShellSort(ElemType data[], int increments[], int n, int incrementsLength)
{
    int i, j, k;
    ElemType tmp;
 for ( k = 0; k < incrementsLength; k++){ // 進行以increments[k]為增量的排序
        for ( i = increments[k]; i < n; i++){
            tmp = data[i];
            for ( j = i; j >= increments[k]; j -= increments[k]){
                if ( tmp >= data[j - increments[k]])
                    break;
                data[j] = data[j - increments[k]];
            }
            data[j] = tmp;
        }
    }
}

補充一個

1.直接插入排序;2.折半插入排序;3.Shell排序;4.冒泡排序;5.快速排序;6.簡單選擇排序;7.堆排序;8.2-路歸併排序;9.2-路歸併排序(非遞迴);10.基數排序等,程式碼如下:

 代碼如下 複製代碼

#include "sort.h"
int main()
{
    cout << "---此程式實現排序---" << endl << endl;
    cout << "1.直接插入排序;2.折半插入排序;3.Shell排序;" << endl;
    cout << "4.冒泡排序;5.快速排序;" << endl;
    cout << "6.簡單選擇排序;7.堆排序;" << endl;
 cout << "8.2-路歸併排序;9.2-路歸併排序(非遞迴);10.基數排序;" << endl;
 cout << "0.退出" << endl << endl;
 
    int n,select,*pData = NULL;
 SLList list;
 int* increments;
   
 while (true){
  cout << "請選擇排序演算法:" << endl;
  cin >> select;
  switch ( select){
  case 1:
   n = init(&pData);
   InsertSort(pData,n);
   break;
  case 2:
   n = init(&pData);
   BInsertSort(pData,n);
   break;
  case 3:       
   int incrementsLenth,i;
   cout << "請輸入增量序列長度:" << endl;
   cin >> incrementsLenth;
   increments = new int[incrementsLenth];
   cout << "請輸入增量序列:" << endl;
   for (i = 0; i < incrementsLenth; i++)
    cin >> increments[i];
   n = init(&pData);
   ShellSort( pData,increments,n,incrementsLenth);
   delete[] increments;
   break;
  case 4:
   n = init(&pData);
   BubbleSort(pData,n);
   break;
  case 5:
   n = init(&pData);
   QuickSort(pData,n);
   break;
  case 6:
   n = init(&pData);
   SelectionSort(pData,n);
   break;
  case 7:
   n = init(&pData);
   HeapSort(pData,n);
   break;
  case 8:
   n = init(&pData);
   MergeSort(pData,n);
   break;
  case 9:
   n = init(&pData);
   MergeSortNonRecursion(pData, n);
   break;
  case 10:
   n = init(&pData);   
   list.Init(pData,n);
   list.RadixSort();
   list.Arrange();
   break;
   // RadixSort(pData,n);
   // break;
  case 0:
   system("pause");
   return 0;
  default:
   cout << "Input Error!"<< endl << endl;
   continue;
  }
  cout << "排序結果為:" << endl;
  if(select == 10)
   cout << list;
  else
   print( pData,0,n - 1);
  cout << endl;
  
  delete[] pData;
 }
    system("pause");
    return 0;
}
int init(int** pData)
{
    int size,i;
    cout << "輸入待排序元素個數" << endl;
    cin >> size;
       
    *pData = new int[size];
   
    cout << "輸入待排序元素"<< endl;   
    for(i = 0 ; i < size; i++)
        cin >> (*pData)[i];
    return size;   
}
template<class ElemType>
void print(ElemType data[],int begin,int end)
{
    for (int i = begin; i <= end; i++){
        cout << data[i] << "  ";
    }
    cout << endl;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.