如何正确地将选择排序算法重新设计为冒泡排序?

时间:2019-04-02 22:55:06

标签: c++ bubble-sort selection-sort

我正在尝试将选择排序转换为冒泡排序。我成功完成了吗?如果没有,我该怎么做才能解决此问题?

我相信我已经正确转换了它。

尝试

// This program demonstrates how to sort and search a
// string vector using selection sort and binary search.
#include <iostream>
#include <string>
#include <vector>
using namespace std;

// Function prototypes
void bubbleSort (vector < string > &);
void swap (string &, string &);
int binarySearch (const vector < string > &, string);

int main ()
{
  string searchValue;       // Value to search for
  int position;         // Position of found value

  // Define a vector of strings.
  vector < string > names
  {
  "Lopez", "Smith", "Pike", "Jones",
      "Abernathy", "Hall", "Wilson", "Kimura",
      "Alvarado", "Harrison", "Geddes", "Irvine"};

  // Sort the vector.
  bubbleSort (names);

  // Display the vector's elements.
  cout << "Here are the sorted names:\n";
for (auto element:names)
    cout << element << endl;
  cout << endl;

  // Search for a name.
  cout << "Enter a name to search for: ";
  getline (cin, searchValue);
  position = binarySearch (names, searchValue);

  // Display the results.
  if (position != -1)
    cout << "That name is found at position " << position << endl;
  else
    cout << "That name is not found.\n";

  return 0;
}

//***********************************************************************
// The selectionSort function sorts a string vector in ascending order. *
//***********************************************************************
void bubbleSort (vector < string > &v)
{
  int minIndex;
  string minValue;

  for (int pass = 1; pass < v.size (); ++pass)
    {
      for (int index = 0; (index + 1) < v.size (); ++index)
    {
      if (v[index + 1] < v[index])
        {
          swap (v[index], v[index + 1]);
        }
    }
    }

//***************************************************
// The swap function swaps a and b in memory.       *
//***************************************************
  void swap (string & a, string & b)
  {
    string temp = a;
    a = b;
    b = temp;
  }

//***************************************************************
// The binarySearch function performs a binary search on a      *
// string vector. array, which has a maximum of size elements,  *
// is searched for the number stored in value. If the number is *
// found, its vector subscript is returned. Otherwise, -1 is    *
// returned indicating the value was not in the vector.         *
//***************************************************************

  int binarySearch (const vector < string > &v, string str)
  {
    int first = 0,      // First vector element
      last = v.size () - 1, // Last vector element
      middle,           // Mid point of search
      position = -1;        // Position of search value
    bool found = false;     // Flag

    while (!found && first <= last)
      {
    middle = (first + last) / 2;    // Calculate mid point
    if (v[middle] == str)   // If value is found at mid
      {
        found = true;
        position = middle;
      }
    else if (v[middle] > str)   // If value is in lower half
      last = middle - 1;
    else
      first = middle + 1;   // If value is in upper half
      }
    return position;
  }

目标是从选择排序转换为冒泡排序。如果有帮助,我拥有尝试转换之前的原始代码。

1 个答案:

答案 0 :(得分:1)

气泡排序仅交换相邻元素,因此在测试非相邻元素的任何地方都将与气泡分离。气泡排序的最简单形式就是这种形式。

for (int pass = 1; pass < v.size(); ++pass) {
    for (int index = 0; (index + 1) < v.size(); ++index) {
        if (v[index + 1] < v[index]) {
            swap(v[index], v[index + 1]);
        }
    }
}

我们可以注意到,在第一次通过之后,最大值必须在正确的位置,类似地,在第二次通过之后,第二最大值位于其位置,依此类推。这样,我们就可以调整内部循环的边界。

for (int pass = 1; pass < v.size(); ++pass) {
    for (int index = 0; (index + pass) < v.size(); ++index) {
        if (v[index + 1] < v[index]) {
            swap(v[index], v[index + 1]);
        }
    }
}