最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++中的幾種排序算法

 更新時間:2014年02月24日 15:27:01   作者:  
這篇文章主要介紹了C++中的幾種排序算法,需要的朋友可以參考下

SortAlgorithm.h

復(fù)制代碼 代碼如下:

#include <vector>
using namespace std;

class SortAlgorithm
{
public:
    SortAlgorithm(int = 10);
    void displayVector();
    void swap(int &, int &);

    void insertSort();                     //O(n^2)
    void selectSort();                    //O(n^2)
    void mergeSort();                    //O(n log n)
    void bubbleSort();                  //O(n^2)
    void quickSort(  int , int  );     //worst: O(n^2),  best: O(n log n)

    int partition( int , int  );
    void sortSubVector(int , int );
    void merge(int , int , int , int );
private:
    int size;
    vector< int > source;
    vector< int > temp;

};

SortAlgorithm.cpp

復(fù)制代碼 代碼如下:

#include <iostream>
#include <cstdlib> // prototypes for functions srand and rand
#include <ctime> // prototype for function time
#include <algorithm> // prototype for sort function
#include "SortAlgorithm.h" // class BinarySearch definition
using namespace std;

SortAlgorithm::SortAlgorithm(int vectorSize)
{
    size = ( vectorSize > 0 ? vectorSize : 10 ); // validate vectorSize
    srand( time( 0 ) ); // seed using current time

   // fill vector with random ints in range 10-99
    for ( int i = 0; i < size; i++ )
           source.push_back( 10 + rand() % 90 ); // 10-99

    temp = source;
}

void SortAlgorithm::insertSort()
{
    int insert;
    for(int next = 1; next < size; next++){
        insert = temp[next];
        int moveItem = next;

        while((moveItem > 0) && (temp[moveItem - 1] > insert)){
            temp[moveItem] = temp[moveItem - 1];
            moveItem--;
        }

        temp[moveItem] = insert;
    }
}

void SortAlgorithm::selectSort()
{
    int loop = size - 1;
    int smallest;

    for(int i = 0; i < loop; i++){
        smallest = i;

        for(int j = i + 1; j < size; j++){
            if(temp[j] < temp[smallest])
                smallest = j;
        }

        swap(temp[i], temp[smallest]);
    }
}

void SortAlgorithm::mergeSort()
{
    sortSubVector(0, size - 1);
}

void SortAlgorithm::bubbleSort()
{
       int comp; // used to control for loop and for subscripts
       bool swapCheck = true; // was a swap made?

    for ( int pass = 1; pass < size && swapCheck; pass++ ) {
        swapCheck = false; // assume no swaps will be made

      // traverse and compare unsorted part of vector
        for ( comp = 0; comp < size - pass; comp++ ){

         // compare adjacent vector elements
            if ( temp[ comp ] > temp[ comp + 1 ] ) {
                swap(temp[comp], temp[comp + 1]);
                swapCheck = true;
                 } // end if

        } // end inner for
    } // end outer for
}

void SortAlgorithm::quickSort(int first, int last )
{
   int currentLocation;

   if ( first >= last )
      return;

   currentLocation = partition( first, last ); // place an element
   quickSort( first, currentLocation - 1 ); // sort left side
   quickSort( currentLocation + 1, last ); // sort right side
} // end function quickSortHelper

// partition the vector into multiple sections
int SortAlgorithm::partition( int left, int right )
{
   int position = left;

   // loop through the portion of the vector
   while ( true )
   {
   //first: from right ro left
      while ( temp[ position ] <= temp[ right ] && position != right )
         --right;

      if ( position == right )
         return position;

      if ( temp[ position ] > temp[ right ])
      {
         swap( temp[ position ], temp[ right ] );
         position = right;
      } // end if
   //second: from left to right
      while ( temp[ left ] <= temp[ position ] && left != position )
         ++left;

      if ( position == left )
         return position;

      if ( temp[ left ] > temp[ position ] )
      {
         swap( temp[ position ], temp[ left ] );
         position = left;
      } // end if
   } // end while
} // end function partition

void SortAlgorithm::sortSubVector(int low, int high)
{
    if((high - low) >= 1){
        int middle1 = (low + high) / 2;
        int middle2 = middle1 + 1;

        /*cout << "split:   ";
        displaySubVector(low, high);
        cout << endl << "       ";
        displaySubVector(low, middle1);
        cout << endl << "       ";
        displaySubVector(middle2, high);
        cout << endl << endl;*/

        sortSubVector(low, middle1);
        //cout << "Stop here1. low = " << low << ", middle1 = " << middle1 << endl;
        sortSubVector(middle2, high);
        //cout << "Stop here2. middle2 = " << middle2 << ", high = " << high << endl;

        merge(low, middle1, middle2, high);

    }
}

void SortAlgorithm::merge(int left, int middle1, int middle2, int right)
{
    int leftIndex = left;
    int rightIndex = middle2;
    int combinedIndex = left;
    vector<int> combined(size);

    /*cout << "merge:   ";
    displaySubVector(left, middle1);
    cout << endl << "       ";
    displaySubVector(middle2, right);
    cout << endl;*/

    while(leftIndex <= middle1 && rightIndex <= right){
        if(temp[leftIndex] <= temp[rightIndex])
            combined[combinedIndex++] = temp[leftIndex++];
        else
            combined[combinedIndex++] = temp[rightIndex++];
    }

    if(leftIndex == middle2){
        while(rightIndex <= right)
            combined[combinedIndex++] = temp[rightIndex++];
    }
    else{
        while(leftIndex <= middle1)
            combined[combinedIndex++] = temp[leftIndex++];
    }

    for(int i = left; i <= right; i++)
        temp[i] = combined[i];

    /*cout << "       ";
    displaySubVector(left, right);
    cout << endl << endl;*/
}

void SortAlgorithm::swap(int &x, int &y)
{
    int t;

    t = x;
    x = y;
    y = t;
}

void SortAlgorithm::displayVector()
{
    for(int i = 0; i < size; i++){
        cout << " " << temp[i];
        if((i + 1) % 10 == 0)
            cout << endl;
    }

    cout << endl;

    temp = source;
}

main.cpp

復(fù)制代碼 代碼如下:

#include <iostream>
#include "SortAlgorithm.h" // class BinarySearch definition
#include "BucketSort.h"
using namespace std;

int main()
{
    int num;
    cout << "Please input the integer number you want to sort:  ";
    cin >> num;

    SortAlgorithm sortVector(num);
    cout << "Unsort elements: \n";
    sortVector.displayVector();

    sortVector.insertSort();
    cout << "\nInsert sorted elements: \n";
    sortVector.displayVector();

    sortVector.selectSort();
    cout << "\nSelect sorted elements: \n";
    sortVector.displayVector();

    sortVector.mergeSort();
    cout << "\nMerge sorted elements: \n";
    sortVector.displayVector();

    sortVector.bubbleSort();
    cout << "\nBubble sorted elements: \n";
    sortVector.displayVector();

    sortVector.quickSort(0, num - 1);
    cout << "\nQuick sorted elements: \n";
    sortVector.displayVector();

   /*BucketSort bucketSortVector( num ); // create BucketSort object

   cout << "Vector elements in original order:\n";
   bucketSortVector.displayElements();

   bucketSortVector.sort(); // sort the vector

   cout << "\nVector elements in sorted order:\n";
   bucketSortVector.displayElements();*/
}

相關(guān)文章

  • C語言實現(xiàn)順序表的順序查找和折半查找

    C語言實現(xiàn)順序表的順序查找和折半查找

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)順序表的順序查找和折半查找,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • include包含頭文件的語句中,雙引號和尖括號的區(qū)別(詳解)

    include包含頭文件的語句中,雙引號和尖括號的區(qū)別(詳解)

    下面小編就為大家?guī)硪黄猧nclude包含頭文件的語句中,雙引號和尖括號的區(qū)別(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • C++ COM編程之接口背后的虛函數(shù)表

    C++ COM編程之接口背后的虛函數(shù)表

    這篇文章主要介紹了C++ COM編程之接口背后的虛函數(shù)表,COM的背后,就是接口,而接口的背后,就是虛函數(shù)表,需要的朋友可以參考下
    2014-10-10
  • C++中IO多路復(fù)用(select、poll、epoll)的實現(xiàn)

    C++中IO多路復(fù)用(select、poll、epoll)的實現(xiàn)

    I/O多路復(fù)用是一種并發(fā)處理多個I/O操作的機(jī)制,本文主要介紹了C++中IO多路復(fù)用(select、poll、epoll)的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • C++實現(xiàn)LeetCode(97.交織相錯的字符串)

    C++實現(xiàn)LeetCode(97.交織相錯的字符串)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(97.交織相錯的字符串),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C++中的String的常用函數(shù)用法(最新推薦)

    C++中的String的常用函數(shù)用法(最新推薦)

    這篇文章主要介紹了C++中的String的常用函數(shù)用法總結(jié),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-02-02
  • Qt實現(xiàn)UDP通信的示例代碼

    Qt實現(xiàn)UDP通信的示例代碼

    UDP是一個輕量級、不可靠、面向數(shù)據(jù)報的、無連接的傳輸層協(xié)議,多用于可靠性要求不嚴(yán)格,不是非常重要的傳輸,如直播、視頻會議等等。本文將通過Qt實現(xiàn)UDP通信,感興趣的可以了解一下
    2022-11-11
  • 解決C語言輸入單個字符屏蔽回車符的問題

    解決C語言輸入單個字符屏蔽回車符的問題

    這篇文章主要介紹了解決C語言輸入單個字符屏蔽回車符的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C語言算法練習(xí)之?dāng)?shù)組求素數(shù)

    C語言算法練習(xí)之?dāng)?shù)組求素數(shù)

    這篇文章主要為大家介紹了C語言算法練習(xí)中數(shù)組求素數(shù)的實現(xiàn)方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C語言有一定幫助,需要的可以參考一下
    2022-09-09
  • C語言操作XML文件的技術(shù)和方法

    C語言操作XML文件的技術(shù)和方法

    XML因其靈活性和標(biāo)準(zhǔn)化特性,被廣泛應(yīng)用于數(shù)據(jù)交換、配置文件、Web服務(wù)等領(lǐng)域,C語言作為一種高效、底層的編程語言,在處理XML數(shù)據(jù)方面也有廣泛的應(yīng)用,本文將深入探討C語言操作XML的技術(shù)和方法,需要的朋友可以參考下
    2025-03-03

最新評論

吉安县| 莱州市| 博乐市| 芦溪县| 湟中县| 东山县| 肃宁县| 清水河县| 通河县| 弥渡县| 吴江市| 茂名市| 宝鸡市| 浮山县| 电白县| 博白县| 孝感市| 上虞市| 永和县| 定襄县| 定西市| 宁陕县| 乐昌市| 沁水县| 息烽县| 东阳市| 额敏县| 台前县| 庆城县| 太仓市| 香格里拉县| 德阳市| 林甸县| 电白县| 牟定县| 平陆县| 安吉县| 肇庆市| 宁安市| 汪清县| 永安市|