简体   繁体   English

在基于数组的列表上实现选择排序

[英]Implementing a Selection Sort on an Array Based List

My question is what am I doing wrong. 我的问题是我在做什么错。 The insert function doesn't work correctly. 插入功能无法正常工作。 I am unable to retrieve the list itself. 我无法检索列表本身。 And because of this my selection sort won't execute. 因此,我的选择排序将不会执行。 Any help would be appreciated. 任何帮助,将不胜感激。

Header: 标头:

/** @file ListA.h */
#include <string>
using namespace std;
const int MAX_LIST = 100;
typedef string ListItemType;
class List
{

public:
   List();
   bool isEmpty() const;
   int getLength() const;
   void insert(int index, const ListItemType& newItem, bool& success);
   void retrieve(int index, ListItemType& dataItem, bool & success) const;
   void remove(int index, bool& success);
   int selectionSortArray(int numdata[], int xnums);
   List selectionSort(List selectList);
private:
    ListItemType items[100];
    int size;
    int translate(int index) const;
};

CPP: CPP:

/** @file ListA.cpp */
#include "ArrayList.h"  // header file
#include <iostream>
#include <fstream>
List::List() : size()
{
}
bool List::isEmpty() const
{
   return size == 0;
}
int List::getLength() const
{
   return size;
}
void List::insert(int index, const ListItemType& newItem, bool& success)
{
   success = (index >= 1) &&
             (index <= size + 1) &&
             (size < MAX_LIST);
   if (success)
   {
      for (int pos = size; pos >= index; --pos)

         items[translate(pos + 1)] = items[translate(pos)];
      items[translate(index)] = newItem;

      ++size;

   }

}
void List::remove(int index, bool& success)
{
   success = (index >= 1) && (index <= size);
   if (success)
   {
      for (int fromPosition = index + 1;
       fromPosition <= size;
       ++fromPosition)
         items[translate(fromPosition - 1)] = items[translate(fromPosition)];
      --size;  // decrease the size of the list by one
   }  // end if

}  // end remove

void List::retrieve(int index, ListItemType& dataItem,
bool& success) const
{
   success = (index >= 1) && (index <= size);
   if (success)
      dataItem = items[translate(index)];
}

int List::translate(int index) const
{
   return index - 1;
}

//List List::selectionSort(List selectList)

//{


//return selectList;


int List::selectionSortArray(int numdata[], int xnums)
{
int tmp;
for (int i = 0; i < xnums -1; i++)
        for (int j = i+1; j < xnums; j++)
            if (numdata[i] > numdata[j])
            {
                tmp = numdata[i];
                numdata[i] = numdata[j];
                numdata[j] = tmp;
            }
    //for( int i = 0; i < 5; i++)
        //cout << numdata[i] << " "; 
        return *numdata;
}

 int main()
 {
 ListItemType insertType = "listItem1";
 ListItemType retrieveType = "listitem2";
 int numberofitems;
 cout << "Please enter the number of data items:" << endl;
 cin >> numberofitems;
 cout << endl;
 cout << "Please enter the data items, one per line:" << endl;
 int listofitems[numberofitems];
 List myArrayList;
 cout << myArrayList.getLength() << endl;
        if (myArrayList.isEmpty())  // tests before
    {
        cout << "This list is empty \n" << endl;
    }
    else
    {
        cout << "List is not empty! \n"<< endl;
    }
 bool mainsucc = true;
 for (int i = 0; i<numberofitems; i++)
 {
 cout << "Enter number " << i+1 << " : " << endl;
 cin >> listofitems[i];
 }
 for (int i =0; i <numberofitems; i++){
 myArrayList.insert(listofitems[i],  insertType, mainsucc);}
 cout << "Size of the list is : " << myArrayList.getLength() << endl;
int listRetrieveSize = myArrayList.getLength();
int listRetrieve[listRetrieveSize];
for (int j=0; j<listRetrieveSize; j++)
 {
 myArrayList.retrieve(listofitems[j], retrieveType, mainsucc);
}
if (myArrayList.isEmpty())  // tests after
    {
        cout << "This list is empty \n" << endl;
    }
    else
    {
        cout << "List is not empty! \n"<< endl;
    }
numberofitems= myArrayList.selectionSortArray(listofitems, numberofitems);
for (int i=0;i>numberofitems;i++)
{
cout<<listofitems[i];
}
 return 1;
 }

Your selection sort is definitely wrong: 您的选择排序绝对是错误的:

int List::selectionSortArray(int numdata[], int xnums)
{
   int tmp; 
   for (int i = 0; i < xnums -1; i++) {
      //you should find the smallest integer inside this for loop
      for (int j = i+1; j < xnums; j++) {
          if (numdata[i] > numdata[j])
          {
              //why do you swap inside this for loop?
              tmp = numdata[i];
              numdata[i] = numdata[j];
              numdata[j] = tmp;
          }
      }//missing closing }
      //smap numdata[i] with currently smallest int

   }//missing closing }
  return *numdata;
}

Your insert function deals with strings, but you are doing selection sort on integers. 您的插入函数处理字符串,但是您正在对整数进行选择排序。

 typedef string ListItemType;

defines string as ListItemType . string定义为ListItemType

我可以肯定您实现的是Bubble排序算法 ,而不是选择排序算法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM