存储桶排序字符串数组和合并排序的存储桶。索引问题

时间:2018-02-11 22:06:24

标签: c# arrays sorting

以下是它能够运行的代码(有输出问题):

另一个"问题"当我输出用循环排序的桶时,它输出"空白"数组空间(如预期的那样)。我怎么能告诉程序"跳过"输出到控制台的空条目? (有点失落的atm)几个小时都在努力......

Bucket sort丢失原始数组中的项目

程序获取程序中的随机数据,并按姓氏排序到存储桶,然后排序"存储桶"用泡沫排序。

提前感谢任何人。

当前问题位于行:

int y = 0;
for (int a = 0; a < (studentInfo.Length - 1); a++)
{ //no idea...Errors at a = 14... because the a + 1 index...so uh
if (String.Compare(studentInfo[a].studentLastName, studentInfo[a + 1].studentLastName) > 0)
    bucketOne[y] = studentInfo[a].studentLastName;
else
    bucketTwo[y] = studentInfo[a].studentLastName;
    y++;
}

全/:

class Program
{
    struct StudentIDs
    {
        public int studentIDs;
        public string studentLastName;
        public string studentFirstName;
    }
    struct UserStudentIDs
    {
        public int UserstudentIDs;
        public string UserstudentLastName;
        public string UserstudentFirstName;
    }
    static void Main(string[] args)
    {
        StudentIDs[] studentInfo = new StudentIDs[15];

        studentInfo[0].studentIDs = 331;
        studentInfo[0].studentLastName = "Thomas";
        studentInfo[0].studentFirstName = "Joe";

        studentInfo[1].studentIDs = 225;
        studentInfo[1].studentLastName = "Smith";
        studentInfo[1].studentFirstName = "Lisa";

        studentInfo[2].studentIDs = 002;
        studentInfo[2].studentLastName = "Jones";
        studentInfo[2].studentFirstName = "Tina";

        studentInfo[3].studentIDs = 333;
        studentInfo[3].studentLastName = "White";
        studentInfo[3].studentFirstName = "Bryan";

        studentInfo[4].studentIDs = 998;
        studentInfo[4].studentLastName = "Huff";
        studentInfo[4].studentFirstName = "John";

        studentInfo[5].studentIDs = 500;
        studentInfo[5].studentLastName = "Street";
        studentInfo[5].studentFirstName = "Zak";

        studentInfo[6].studentIDs = 772;
        studentInfo[6].studentLastName = "Abel";
        studentInfo[6].studentFirstName = "Karen";

        studentInfo[7].studentIDs = 222;
        studentInfo[7].studentLastName = "Streit";
        studentInfo[7].studentFirstName = "Susan";

        studentInfo[8].studentIDs = 332;
        studentInfo[8].studentLastName = "Thomas";
        studentInfo[8].studentFirstName = "Dan";

        studentInfo[9].studentIDs = 141;
        studentInfo[9].studentLastName = "Bryient";
        studentInfo[9].studentFirstName = "Bill";

        studentInfo[10].studentIDs = 880;
        studentInfo[10].studentLastName = "Forte";
        studentInfo[10].studentFirstName = "Mary";

        studentInfo[11].studentIDs = 900;
        studentInfo[11].studentLastName = "Ferguson";
        studentInfo[11].studentFirstName = "Fran";

        studentInfo[12].studentIDs = 220;
        studentInfo[12].studentLastName = "Worke";
        studentInfo[12].studentFirstName = "Elaine";

        studentInfo[13].studentIDs = 635;
        studentInfo[13].studentLastName = "Knapp";
        studentInfo[13].studentFirstName = "Justin";

        studentInfo[14].studentIDs = 445;
        studentInfo[14].studentLastName = "Dustin";
        studentInfo[14].studentFirstName = "Tom";

        WriteLine("              STUDENT INFO: (Unsorted Data) ");
        for (int i = 0; i < studentInfo.Length; i++)
        {
            WriteLine("Student ID #: {0} ------ Last Name: {1} -- First Name: {2} ",
                studentInfo[i].studentIDs, studentInfo[i].studentLastName, studentInfo[i].studentFirstName);
        }
        Write("\nPress Enter To Continue! ");
        ReadLine();
        string[] bucketOne = new string[15];
        string[] bucketTwo = new string[15];
        int y = 0;
        for (int a = 0; a < (studentInfo.Length - 1); a++)
        {//no idea...Errors at a = 14... because the a + 1 index...so uh
            if (String.Compare(studentInfo[a].studentLastName, studentInfo[a + 1].studentLastName) > 0)
                //if ((studentInfo[a].studentLastName.CompareTo(studentInfo[a + 1].studentLastName)) > 0)
                bucketOne[y] = studentInfo[a].studentLastName;
            else
                bucketTwo[y] = studentInfo[a].studentLastName;
            y++;
        }
        //sort two buckets now with bubble sorting counts swaps and compare
        int swap = 0,
            compare = 0;
        string tempString = " ";
        bool Swapping;
        do
        {
            Swapping = false;
            for (int index = 0; index < (studentInfo.Length - 1); index++)
            {
                compare++;
                if (string.Compare(bucketOne[index], bucketOne[index + 1]) < 0)
                {
                    tempString = bucketOne[index];
                    bucketOne[index] = bucketOne[index + 1];
                    bucketOne[index + 1] = tempString;
                    swap++;
                    Swapping = true;
                }
            }
        } while (Swapping == true);
        //Bubble Sort Two
        int swapTwo = 0,
            compareTwo = 0;
        string tempStringTwo = " ";
        bool SwappingTwo;
        do
        {
            SwappingTwo = false;
            for (int index = 0; index < (studentInfo.Length - 1); index++)
            {
                compareTwo++;
                if (string.Compare(bucketTwo[index], bucketTwo[index + 1]) < 0)
                {
                    tempStringTwo = bucketTwo[index];
                    bucketTwo[index] = bucketTwo[index + 1];
                    bucketTwo[index + 1] = tempStringTwo;
                    swapTwo++;
                    SwappingTwo = true;
                }
            }
        } while (SwappingTwo == true);
        WriteLine("Bucket One Sorted: ");
        for (int i = 0; i < bucketOne.Length; i++)
        {
            WriteLine("\nLast Names (Bucket One): {0}", bucketOne[i]);
        }
        Write("\nPress Enter To Continue! ");
        ReadLine();
        WriteLine("\nBucket Two Sorted: ");
        for (int i = 0; i < bucketTwo.Length; i++)
        {
            WriteLine("\nLast Names (Bucket Two): {0}", bucketTwo[i]);
        }
        Write("\nPress Enter To Continue! ");
        ReadLine();
        string[] FullBucket = bucketOne.Union(bucketTwo).ToArray(); // would remove any of the "same" name...
        Array.Reverse(FullBucket);
        foreach (var StudentLastName in FullBucket)
        {
            WriteLine(StudentLastName);
        }
        //string[] FullBucket = new string[15];
        //for (int i = 0; i < FullBucket.Length; i++)
        //{
        //    if (i >= bucketOne.Length) //- bucketOne.Length
        //        FullBucket[i] = bucketTwo[i];
        //    else
        //        FullBucket[i] = bucketOne[i];
        //}
        //string[] FullBucket = bucketOne.Concat(bucketTwo).ToArray();
        //string[] FullBucket = new string[15];
        //int numOne = 0, //i
        //    numTwo = 0, //j
        //    numThree = 0; //k
        //// Traverse both array
        //while (numOne < bucketOne.Length && numTwo < bucketTwo.Length)
        //{
        //    // Check if current element of first
        //    // array is smaller than current element
        //    // of second array. If yes, store first
        //    // array element and increment first array
        //    // index. Otherwise do same with second array
        //    if (string.Compare(bucketOne[numOne], bucketTwo[numTwo]) < 0)//???????????????
        //        FullBucket[numThree = numThree + numThree] = bucketOne[numOne++];
        //    else
        //        FullBucket[numThree++] = bucketTwo[numTwo++];
        //}
        //// Store remaining elements of first array
        //while (numOne < bucketOne.Length)
        //    FullBucket[numThree++] = bucketOne[numOne++];

        //// Store remaining elements of second array
        //while (numTwo < (bucketTwo.Length - 1))
        //    FullBucket[numThree++] = bucketTwo[numTwo++];
        //WriteLine("              \nSTUDENT INFO: (Sorted Data) ");
        //for (int i = 0; i < (FullBucket.Length - 1); i++) //Length -1 ?????
        //{
        //    WriteLine("Student ID #: {0} ------ Last Name: {1} -- First Name: {2} ",
        //        studentInfo[i].studentIDs, studentInfo[i].studentLastName, studentInfo[i].studentFirstName);
        //}
        //ReadLine();

        UserStudentIDs[] UserstudentInfo = new UserStudentIDs[20];
        int RecordIndex = 0;
        WriteLine("Enter Student ID #: ");
        UserstudentInfo[RecordIndex].UserstudentIDs = Convert.ToInt32(ReadLine());
        WriteLine("You May Enter Up To 20 Records: (Or 999 to Finish)");
        while (UserstudentInfo[RecordIndex].UserstudentIDs != 999)
        {
            WriteLine("Enter Student First Name: ");
            UserstudentInfo[RecordIndex].UserstudentFirstName = ReadLine();
            WriteLine("Enter Student Last Name: ");
            UserstudentInfo[RecordIndex].UserstudentLastName = ReadLine();
            RecordIndex++;
            WriteLine("Enter Student ID #: ");
            UserstudentInfo[RecordIndex].UserstudentIDs = Convert.ToInt32(ReadLine());
        }
        WriteLine("              USER STUDENT INFO: (Unsorted Data) ");
        for (int i = 0; i < RecordIndex; i++) //or user..info.length...etc
        {
            WriteLine("Student ID #: {0} ------ Last Name: {1} -- First Name: {2} ",
                UserstudentInfo[i].UserstudentIDs, UserstudentInfo[i].UserstudentLastName, UserstudentInfo[i].UserstudentFirstName);
        }
        Write("\nPress Enter To Continue! ");
        ReadLine();
        string[] bucketThree = new string[20];
        string[] bucketFour = new string[20];
        int L = 0;
        for (int a = 0; a < RecordIndex; a++) // or ? (studentInfo.Length - 1)
        {//no idea here again....
            if (String.Compare(UserstudentInfo[a].UserstudentLastName, UserstudentInfo[a + 1].UserstudentLastName) > 0)
                bucketThree[L] = UserstudentInfo[a].UserstudentLastName;
            else
                bucketFour[L] = UserstudentInfo[a].UserstudentLastName;
            L++;
        }
        int swapThree = 0,
            compareThree = 0;
        string tempStringThree = " ";
        bool SwappingThree;
        do
        {
            SwappingThree = false;//(studentInfo.Length - 1)
            for (int index = 0; index < RecordIndex; index++)
            {
                compareThree++;
                if (string.Compare(bucketThree[index], bucketThree[index + 1]) < 0)
                {
                    tempStringThree = bucketThree[index];
                    bucketThree[index] = bucketThree[index + 1];
                    bucketThree[index + 1] = tempStringThree;
                    swapThree++;
                    SwappingThree = true;
                }
            }
        } while (SwappingThree == true);
        int swapFour = 0,
            compareFour = 0;
        string tempStringFour = " ";
        bool SwappingFour;
        do
        {
            SwappingFour = false; //(studentInfo.Length - 1) or record index?
            for (int index = 0; index < RecordIndex; index++)
            {
                compareFour++;
                if (string.Compare(bucketFour[index], bucketFour[index + 1]) < 0)
                {
                    tempStringFour = bucketFour[index];
                    bucketFour[index] = bucketFour[index + 1];
                    bucketFour[index + 1] = tempStringFour;
                    swapFour++;
                    SwappingFour = true;
                }
            }
        } while (SwappingFour == true);

        WriteLine("Bucket Three Sorted: ");
        for (int i = 0; i < bucketThree.Length; i++)
        {
            WriteLine("\nLast Names (Bucket One): {0}", bucketThree[i]);
        }
        Write("\nPress Enter To Continue! ");
        ReadLine();
        WriteLine("\nBucket Four Sorted: ");
        for (int i = 0; i < bucketFour.Length; i++)
        {
            WriteLine("\nLast Names (Bucket Two): {0}", bucketFour[i]);
        }
        Write("\nPress Enter To Continue! ");
        ReadLine();
        string[] UserFullBucket = bucketThree.Union(bucketFour).ToArray();
        Array.Reverse(UserFullBucket);
        foreach (var UserStudentLastName in UserFullBucket)
        {
            WriteLine(UserStudentLastName);
        }
        ReadLine();
    }
}

0 个答案:

没有答案