Question

Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm...

Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm should be able to sort an array like this: Input: an array that has n elements, and the values of its elements are assigned randomly, for example: Index 0 1 2 3 4 5 value 7 3 6 2 1 5 Output: an array - its first n/2 elements are sorted in ascending order and its second n/2 elements sorted in descending order. That is, after sorting, the array above will become: Index 0 1 2 3 4 5 value 5 6 7 3 2 1 As the solution of this problem, you need to submit the following answer(s): (1). Your algorithm written in pseudo code . (2). The implementation of your algorithm in C# .

0 0
Add a comment Improve this question Transcribed image text
Answer #1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace wk5collabproject
{
class Program
{
static void Main(string[] args)
{

//int [] myArray = new int[] { 5,2,4, 7, 9, 3, 1, 6, 8 };
int [] myArray = new int[] { 5,2,3,4,5,6,7,8 };
int middle, temp , curr;
if(myArray.Length%2==0)
middle = myArray.Length/2;
else
middle = (myArray.Length+1)/2;

for(int i=0; i<myArray.Length; ++i){
if(i<middle){
curr = i;
for(int j=i+1; j<middle;++j){

if(myArray[curr] > myArray[j]){
temp = myArray[curr];
myArray[curr]=myArray[j];
myArray[j] = temp;
}

}
}
else {
curr = i;
for(int k=i+1; k<myArray.Length; ++k){
if(myArray[curr]<myArray[k]){
curr = k;
}
temp = myArray[i];
myArray[i]=myArray[curr];
myArray[curr]= temp;
}
}
}
for(int i=0;i<myArray.Length;++i){
Console.WriteLine(myArray[i] + " ");
}
}
}
}

================================================================
See Ouput

class Program 2 3 static void Main(string[] args) Ant myArray - new int 5,2,4, 7, 9, 3, 1, 6, 8; 5,2,3,4,5,6,7,8 ; 6 int myAr

Thanks

Add a comment
Answer #2

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace Selection_Sort

{

    class Program

    {

        static void Main(string[] args)

        {

            Selection_Sort selection = new Selection_Sort(10);

            selection.Sort();

        }

    }


    class Selection_Sort

    {

        private int[] data;

        private static Random generator = new Random();

        //Create an array of 10 random numbers

           public Selection_Sort(int size)

        {

            data = new int[size];

            for (int i = 0; i < size; i++)

            {

                data[i] = generator.Next(20, 90);

            }

        }


        public void Sort()

        {

            Console.Write("\nSorted Array Elements :(Step by Step)\n\n");

display_array_elements();

int smallest; 

            for (int i = 0; i < data.Length - 1; i++)

            {

                smallest = i;


                for (int index = i + 1; index < data.Length; index++)

                {

                    if (data[index] < data[smallest])

                    {

                        smallest = index;

                    }

                }

                Swap(i, smallest);

                display_array_elements();

   }

           

        }


        public void Swap(int first, int second)

        {

            int temporary = data[first];    

            data[first] = data[second];  

            data[second] = temporary;  

        }  


        public void display_array_elements()

        {        

            foreach (var element in data)

            {

                Console.Write(element + " ");

            }

            Console.Write("\n\n");

        }

    }

}


answered by: Shivani Sharma
Add a comment
Know the answer?
Add Answer to:
Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort...

    Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 Initial List 4 6 18 4 6 Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position:...

  • please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection...

    please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 4 6 Initial List Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position: Sorted List...

  • Selection Sort is a common algorithm to sort the elements of an array. First, it scans...

    Selection Sort is a common algorithm to sort the elements of an array. First, it scans the elements of the array to find the smallest value and places it at index 0, thus creating a sorted “subarray” of size 1 that contains the smallest value. Then it scans the remaining unsorted values for the new smallest value and places it at index 1, creating a sorted subarray of size 2 that contains the 2 smallest values. It continues in this...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • 2. Suggest a structured plan (algorithm) for the bubble sort and selection sort, and perform running...

    2. Suggest a structured plan (algorithm) for the bubble sort and selection sort, and perform running time analysis for both best and worst case. 3. Consider the age data of 12 children who are supposed to undergo for vaccination in ascending order of their age. Suggest and apply a sorting technique which can efficiently handle this data. Show all the intermediate steps clearly. Child ID 01 02 03 04 05 06 07 08 09 10 11 12 2. Age 1...

  • C++ Search & Sort

    Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides.  Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array.  Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define...

  • Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

    Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter {    /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort    */    public static void sort(int[] arr)    { // Your...

  • 3. Modify the insertion sort algorithm discussed in class so that it can sort strings. That...

    3. Modify the insertion sort algorithm discussed in class so that it can sort strings. That is, its input will be an array, the type of which is string. The insertion sort algorithm will sort the elements in this array. Finally, its output will be a sorted array. As the solution of this problem, you need to submit the following answer(s): (1). The implementation of your algorithm in C# (20points). Algorithm: At each array-position, it checks the value there against...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT