vendredi 26 octobre 2018

Creating 1000 arrays and sorting them using the bubble and selection sort (C#)

I am new to programming. C# is my first programming language.

I have an assignment where I have to create and test out a bubble sort algorithm and a selection sort algorithm using arrays. I think I understand those now.

The next part of the assignment I am having some trouble on.

I have to write a program that will ask the user for a number (n) and create 1000 arrays of n size.

So if the user enters 5 for the number, my program has to create and sort 1000 arrays that are of length 5.

I have to use the bubble sort and the selection sort methods I created.

After I do that, I have to initiate a variable called running_time to 0. I have to create a for loop that iterates 1000 times and in the body of the loop i have to create an array of n random integers.

Then I have to get the time and set this to the start time. My professor said to notice that the sort is started after each array is built, so I should time the sort process only.

Then I have to get the time and set it to end time. I have to subtract the start time from end time and add the result to the total time.

Once the program has run, note 1. the number of items sorted 2. the average running time for each array (total time/1000)

Then I have to repeat the process using 500, 2500, and 5000 as the size of the array.

This is my code for creating one array with n number of spaces and filled with random integers.

//Asks the user for number
        Console.WriteLine("Enter a number: ");
        n = Convert.ToInt32(Console.ReadLine());

        //Creates an array of the length of the user entered number
        int[] randArray = new int[n];

        //Brings in the random class so we can use it.
        Random r = new Random();

        Console.WriteLine("This is the array: ");

//For loop that will put in a random number for each spot in the array. 
        for (int i = 0; i < randArray.Length; i++) {
            randArray[i] = r.Next(n);
            Console.Write(randArray[i] + " ");
        }
        Console.WriteLine();

THIS IS MY CODE FOR THE BUBBLE SORT ALGORITHM:

//Now performing bubble sort algorithm:
        for (int j = 0; j <= randArray.Length - 2; j++) {
            for (int x = 0; x <= randArray.Length - 2; x++) {
                if (randArray[x] > randArray[x + 1]) {
                    temp = randArray[x + 1];
                    randArray[x + 1] = randArray[x];
                    randArray[x] = temp;
                }
            }
        }

//For each loop that will print out the sorted array
        foreach (int array in randArray) {
            Console.Write(array + " ");
        }
        Console.WriteLine();

THIS IS MY CODE FOR THE SELECTION SORT ALGORITHM:

//Now performing selection sort algorithm
        for (int a = 0; a < randArray1.Length - 1; a++) {
            minkey = a;
            for (int b = a + 1; b < randArray1.Length; b++) {
                if (randArray1[b] < randArray1[minkey]) {
                    minkey = b;
                }
            }
            tempSS = randArray1[minkey];
            randArray1[minkey] = randArray1[a];
            randArray1[a] = tempSS;
        }

//For loop that will print the array after it is sorted.
        Console.WriteLine("This is the array after the selection sort algorithm.");
        for (int c = 0; c < randArray1.Length; c++) {
            Console.Write(randArray1[c] + " ");
        }
        Console.WriteLine();

This is very overwhelming as I am new to this and I am still learning this language.

Can someone guide me on the beginning on how to create 1000 different arrays filled with random numbers and then the rest. I would appreciate it greatly. Thank you.




Aucun commentaire:

Enregistrer un commentaire