C ++的问题冒泡排序二维字符数组 [英] C++ Issues Bubble Sorting a 2D char Array

查看:175
本文介绍了C ++的问题冒泡排序二维字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这问题是关于code我写了一个任务,所以是的,我必须使用冒泡排序,是的,我必须使用数组。

This question is about code I'm writing for an assignment, so yes, I have to use bubble sort, and yes, I have to use arrays.

好吧,所以我会做的很好冒泡排序的一维数组,但是试图理清二是引起了我的悲伤。

Okay, so I be doing fine bubble sorting one dimensional arrays, but trying to sort two is causing me grief.

我所要做的排序是名称(即乔,鲍勃,贝蒂,杰克)按照字母顺序排列的列表。使用下面code程序尝试的事情进行排序(名左右移动),但它通常只是部分正确。

What I am trying to do is sort a list of names (ie. Joe, Bob, Betty, Jake) into alphabetical order. Using the following code the program tries to sort the things (names move around) but it usually is only partially correct.

#include <iostream>
#include <fstream>
using namespace std;
void AlphaSort(char [][50]);
void GetInput(char [][50]);
void PrintArray(char [][50]);

int main()
{
    char name[4][50];
    cout << "Please enter 4 names:\n";
    GetInput(name);
    cout << endl << "The following names were received:\n";
    PrintArray(name);
    cout << endl << "The names will now be sorted.\n\n"
        << "Calling Sort Function....\n";
    AlphaSort(name);
    cout << endl << "The sorted name order is now:\n";
    PrintArray(name);

    return 0;
}

void GetInput(char name[][50])
{
    int i;

    for(i=0; i<4; i++)
        cin.getline(name[i], 50);
}

void PrintArray(char name[][50])
{
    int i;

    for(i=0; i<4; i++)
        cout << name[i] << endl;
}

void AlphaSort(char name[][50]) //I'm pretty sure the problem is in here.
{
    int Nnames = 4, pass, column, row, letter, sorted;
    char temp[50];

    cout << "Sorting Function Successfully Called...\n"
        << endl << "Names recieved by sort function:\n";

    PrintArray(name);

    //Specifically in this attempt at a bubble sort.
    for(pass=0; pass<(Nnames-1); pass++)
    {
        for(row=0; row<(Nnames-pass-1); row++)
        {
            for(column=0, sorted=0; sorted==0; column++)
            {
                if(name[row][column]>name[row+1][column])
                {
                    for(letter=0; letter<50; letter++)
                        temp[letter] = name[row][letter];
                    for(letter=0; letter<50; letter++)
                        name[row][letter]= name[row+1][letter];
                    for(letter=0; letter<50; letter++)
                        name[row+1][letter] = temp[letter];
                    sorted = 1;
                }
                else if (name[row][column] == name[row+1][column]);
            }
        }
    }    

    cout << endl << "Name order post sort:\n";

    PrintArray(name);
}

运行这个目前看起来像这样平时。​​

Running this currently looks something like this usually.

请输入4名:Hammy哈利·阿什亚伦

Please enter 4 names: Hammy Harry Ashe Aaron

收到以下名字:Hammy哈利·阿什亚伦

The following names were received: Hammy Harry Ashe Aaron

该名称将现在进行排序。

The names will now be sorted.

调用排序功能....排序函数调用成功...

Calling Sort Function.... Sorting Function Successfully Called...

按排序功能收到名称:Hammy哈利·阿什亚伦

Names recieved by sort function: Hammy Harry Ashe Aaron

名称后序排序:亚伦·阿什哈利Hammy

Name order post sort: Aaron Ashe Harry Hammy

排序后的名称顺序现在是:阿隆·阿什哈利Hammy

The sorted name order is now: Aaron Ashe Harry Hammy

我已经在类似的问题四处寻找,但我想不通为什么它是愚蠢的。

I've been looking around at similar problems, but I can't figure out why it's being silly.

推荐答案

这似乎在我的问题是如何打好我的if语句没有解决它按正确的顺序是已经,这使得它循环,直到它找到的可能性信以后,它希望进行切换。固定code是这样的:

It seems my problem lay in how my if statement didn't address the possibility of it being in the right order already, which made it loop until it found a letter later on that it wanted to switch. The fixed code looks like this:

void AlphaSort(char name[][50])
{
     int Nnames = 4, pass, column, row, letter, sorted;
     char temp[50];

     cout << "Sorting Function Successfully Called...\n"
          << endl << "Names recieved by sort function:\n";

     PrintArray(name);

     for(pass=0; pass<(Nnames-1); pass++)
     {
                 for(row=0; row<(Nnames-pass-1); row++)
                 {
                            for(column=0, sorted=0; sorted==0; column++)
                            {
                                          if(name[row][column]>name[row+1][column])
                                          {
                                                                     for(letter=0; letter<50; letter++)
                                                                              temp[letter] = name[row][letter];
                                                                     for(letter=0; letter<50; letter++)
                                                                              name[row][letter]= name[row+1][letter];
                                                                     for(letter=0; letter<50; letter++)
                                                                              name[row+1][letter] = temp[letter];
                                                                     sorted = 1;
                                          }
                                          else if (name[row][column]<name[row+1][column])//This is the fix.
                                               sorted = 1;
                            }
                 }
     }    

     cout << endl << "Name order post sort:\n";

     PrintArray(name);
}

这篇关于C ++的问题冒泡排序二维字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆