多种排序算法的比较

时间:2018-01-30 19:32:22

标签: c++ algorithm sorting vector random

目前,该程序将生成10个随机数,并排序(从最小到最大),反向排序或随机排序。但是,在尝试列出所进行的比较次数时,打印出的比较次数完全不正确。例如,它打印出有44个与冒泡排序的比较(这是每次变化但通常在40左右),45个具有选择排序,9个具有插入排序。目前我只使用numbersSorted()运行程序只是为了确保比较有效。 如何使用每种排序方法打印正确数量的比较?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int count1 = 0; //bubble
int count2 = 0; //selection
int count3 = 0; //insertion

vector<int> numbersSorted(int n);
vector<int> numbersReversed(int n);
vector<int> numbersShuffled(int n);

int main() {
srand(time(0));



    numbersSorted(10);
  //numbersReversed(10);
  //numbersShuffled(10);


return 0;
}

vector<int> numbersSorted(int n)
{

vector <int> v(n);

for (auto &x : v)
    x = rand() % 100;

cout << "Original list to be sorted: ";
for (auto x : v)
    cout  << x << " ";
cout << "\n\n";

// bubble sort
bool swapped = true;
for (int pass = 0; pass <= n - 2 && swapped; ++pass)
{
    swapped = false;
    for (int i = 0; i <= n - pass - 2; ++i)
    {
        ++count1;
        if (v.at(i) > v.at(i + 1))
        {
            swap(v[i], v[i + 1]);
            swapped = true;
        }
    }
}
cout << "Bubble sort sorted: ";
for (auto x : v)
    cout << x << " ";
cout << "\n";
cout << "There were " << count1 << " comparisons with bubble sort.\n" << 
endl;

// selection sort
for (int pass = 0; pass <= n - 2; ++pass)
{
    // Find least element remaining in the list starting at index pass
    int minIndex = pass;
    // i = minIndex + 1, minIndex + 1, ..., n - 1
    for (int i = minIndex + 1; i < n; i++)
    {
        ++count2;

        if (v[i] < v[minIndex])
        {
            minIndex = i;

        }
            // The element at index i is smaller than what I thought was the 
min
    }

    swap(v[pass], v[minIndex]);
}
cout << "Selection sort sorted: ";
for (auto x : v)
    cout << x << " ";
cout << "\n";
cout << "There were " << count2 << " comparisons with selection sort.\n" << 
endl;


//insertion sort
for (int pass = 0; pass <= n - 2; ++pass) {
    // Take the element at pass+1 and move it to the left until it's in the
    // right spot (i.e., as long as it's in the wrong spot).

    // for i = pass, pass-1, ..., 0 while L[i] > L[i+1]
    ++count3;
    for (int i = pass; i >= 0 && v[i] > v[i + 1]; --i) {
        swap(v[i], v[i + 1]);
    }
}
cout << "Insertion sort sorted: ";
for (auto x : v)
    cout << x << " ";
cout << "\n";
cout << "There were " << count3 << " comparisons with insertion sort.\n" << 
endl;
//return v;
}
vector<int> numbersReversed(int n)
{
vector <int> v(n);

for (auto &x : v)
    x = rand() % 100;

cout << "Original list to be reversed: ";
for (auto x : v)
    cout << x << " ";
cout << "\n\n";


// bubble sort
bool swapped = true;
for (int pass = 0; pass <= n - 2 && swapped; ++pass)
{
    swapped = false;
    for (int i = 0; i <= n - pass - 2; ++i)
    {
        ++count1;
        if (v.at(i) > v.at(i + 1))
        {
            swap(v[i], v[i + 1]);
            swapped = true;
        }
    }
}

//reverse the content of the vector
reverse(v.begin(),v.end());

cout << "Bubble sort reversed: ";
for (auto x : v)
    cout << x << " ";
cout << "\n";
cout << "There were " << count1 << " comparisons with bubble sort.\n" << 
endl;


// selection sort
for (int pass = 0; pass <= n - 2; ++pass)
{
    // Find least element remaining in the list starting at index pass
    int minIndex = pass;
    // i = minIndex + 1, minIndex + 1, ..., n - 1
    for (int i = minIndex + 1; i < n; i++)
    {
        ++count2;
        if (v[i] < v[minIndex])
            // The element at index i is smaller than what I thought was the 
min
            minIndex = i;
    }

    swap(v[pass], v[minIndex]);
}

reverse(v.begin(),v.end());

cout << "Selection sort reversed: ";
for (auto x : v)
    cout << x << " ";
cout << "\n";
cout << "There were " << count2 << " comparisons with selection sort.\n" << 
endl;



// insertion sort
for (int pass = 0; pass <= n - 2; ++pass) {
    // Take the element at pass+1 and move it to the left until it's in the
    // right spot (i.e., as long as it's in the wrong spot).

    // for i = pass, pass-1, ..., 0 while L[i] > L[i+1]
    ++count3;
    for (int i = pass; i >= 0 && v[i] > v[i + 1]; --i) {
        swap(v[i], v[i + 1]);
    }
}

reverse(v.begin(),v.end());
cout << "Insertion sort reversed: ";
for (auto x : v)
    cout << x << " ";
cout << "\n";
cout << "There were " << count3 << " comparisons with insertion sort.\n" << 
endl;

}

vector<int> numbersShuffled(int n)
{
vector<int> v(n);

for (auto &x : v)
{
    x = rand() % 100;
    ++count1;
}


cout << "Numbers Shuffled: ";
for (auto x : v)
    cout << x << " ";
cout << "\n";
cout << "There were " << count1 << " comparisons made. " << endl;
}

0 个答案:

没有答案