Need in C#
Program 0 (30%): Why – just why? One of the least useful sorts that professors never like to talk about (because it’s SOOO inefficient and never used in practice) has two phases: 1) it shuffles the numbers in the array and 2) checks to see if they are in ascending order. Why is this so bad? Because there are n! ways to order an array with n elements. For example, if there are 100 elements in the array, n == 100 and there are 100*99*98*97*…*5*4*3*2*1 different ways to arrange that! Ridiculous? Yes. That’s why you’re going to code it up. How do you shuffle an array? The easiest way is to traverse each element in the array with a loop, then pick a random element/cell to swap it with. Think through how to determine if it’s in order (hint: traverse).
For this assignment, you need to implement only (source code) a program that 1) initializes and array of 5 elements with random values between 1-50 (i.e. no user input), 2) shuffles the array, 3) prints the array, 4) determines if the elements are in ascending (or non-decreasing) order and 5) repeats steps 2-4 until step 4 is true. Below is an edited sample output, only showing the last few lines of output. Hint: stay sane and use functions. Also, since you need to be able to visualize what’s going on, we’d recommend starting with printing the array to the screen. Note: the random number generator below
Sample run 1:
|34|44|28|33|29|
|33|28|29|34|44|
|34|29|44|33|28|
|33|44|28|34|29|
|34|28|29|33|44|
|33|29|44|34|28|
|34|44|28|33|29|
|33|28|29|34|44|
|34|29|44|33|28|
|33|44|28|34|29|
|34|28|29|33|44|
|33|29|44|34|28|
|34|44|28|33|29|
|33|28|29|34|44|
|34|29|44|33|28|
|33|44|28|34|29|
|34|28|29|33|44|
|33|29|44|34|28|
|34|44|28|33|29|
|33|28|29|34|44|
|34|29|44|33|28|
|33|29|28|44|34|
|44|29|34|28|33|
|28|29|33|34|44|
Hi, I have answered this question before, so here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
using System;
class ShuffleSort {
// method to check if an array is sorted
(in asc order)
static bool
isSorted(int[] arr) {
// looping from
index 1 to length-1
for
(int i = 1; i < arr.Length; i++) {
//
if previous element is bigger than current element, array is
not
//
in sorted order, returning false
if
(arr[i - 1] > arr[i]) {
return
false;
}
}
// if all numbers
are in sorted order, returning true
return
true;
}
// method to print elements of an
array
static void
printArray(int[] arr) {
for
(int i = 0; i < arr.Length; i++) {
Console.Write("|"
+ arr[i]);
}
Console.WriteLine("|");
}
// method to shuffle elements of an
array
static void
shuffle(int[] arr) {
Random random =
new Random();
// looping for
array's length number of times
for
(int i = 0; i < arr.Length; i++) {
//
generating a random valid index between 0 and length-1
int
randIndex = random.Next(0,arr.Length);
//
swapping elements at indices randIndex and i
int
temp = arr[randIndex];
arr[randIndex]
= arr[i];
arr[i]
= temp;
}
}
public static void
Main(String[] args) {
// creating an
array of 5 integers, and filling the values
int[]
arr = new int[5];
Random random =
new Random();
for
(int i = 0; i < arr.Length; i++) {
//
generating and assigning integer between 1 and 50 to current
//
location
arr[i]
= random.Next(1,51);
}
// printing
array
printArray(arr);
// looping until
the array is in sorted order
while
(!isSorted(arr)) {
//
shuffling array
shuffle(arr);
//
printing array
printArray(arr);
}
//uncomment below
lines if you want to wait for a key entry to quit.
//Console.WriteLine("\n\n\nPress
any key to quit");
//Console.ReadKey();
}
}
/*OUTPUT*/
|23|27|19|14|25|
|19|14|25|27|23|
|23|14|25|19|27|
|27|14|25|23|19|
|19|14|25|27|23|
|23|14|25|19|27|
|27|14|25|23|19|
|19|14|25|27|23|
|23|14|25|19|27|
|27|14|25|23|19|
|19|14|25|27|23|
|23|14|25|19|27|
|27|14|25|23|19|
|19|14|25|27|23|
|23|14|25|19|27|
|27|14|25|23|19|
|19|14|25|27|23|
|23|14|25|19|27|
|27|14|25|23|19|
|19|14|25|27|23|
|23|14|25|19|27|
|27|14|25|23|19|
|19|14|25|27|23|
|23|14|25|19|27|
|27|14|25|23|19|
|19|14|25|27|23|
|25|19|14|23|27|
|14|25|19|27|23|
|19|14|25|23|27|
|25|19|14|27|23|
|14|25|19|23|27|
|19|14|25|27|23|
|25|19|14|23|27|
|25|23|19|27|14|
|25|27|23|14|19|
|25|14|27|19|23|
|25|14|27|19|23|
|25|14|27|19|23|
|14|27|23|19|25|
|27|23|25|19|14|
|23|25|14|19|27|
|23|19|25|14|27|
|23|14|19|25|27|
|19|23|25|27|14|
|25|19|27|14|23|
|27|25|14|23|19|
|23|25|27|19|14|
|19|25|23|14|27|
|23|14|25|27|19|
|25|27|14|19|23|
|23|27|19|14|25|
|25|27|14|19|23|
|19|23|14|25|27|
|25|27|14|19|23|
|19|23|14|25|27|
|25|27|23|19|14|
|19|14|27|25|23|
|25|23|14|19|27|
|19|27|23|25|14|
|14|19|23|25|27|
Need in C# Program 0 (30%): Why – just why? One of the least useful sorts...
JAVA plz Program 0 (50%): Why – just why? One of the least useful sorts that professors never like to talk about (because it’s SOOO inefficient and never used in practice) has two phases: 1) it shuffles the numbers in the array and 2) checks to see if they are in ascending order. Why is this so bad? Because there are n! ways to order an array with n elements. For example, if there are 100 elements in the array,...
C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...
C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with each element representing its column and row value, starting from 1. So the element at the first column and the first row will be 11. If either length is out of the range, simply return a null. For exmaple, if colLength = 5 and rowLength = 4, you will see: 11 12 13 14 15 21 22 23 24 25 31 32 33 34...
An m×n
array A
of real numbers is a Monge array if for all i,j,k,
and l
such that 1≤i<k≤m
and 1≤j<l≤n
, we have
>A[i,j]+a[k,l]≤A[i,l]+A[k,j]>
In other words, whenever we pick two rows and two columns of a
Monge array and consider the four elements at the intersections of
the rows and columns, the sum of the upper-left and lower-right
elements is less than or equal to the sum of the lower-left and
upper-right elements. For example, the following...
Write a C or C++ program
A6pc(pp) that accepts one command
line argument which is an integer n between 2 and 6
inclusive. Generate a string of 60 random upper case English
characters and store them somewhere (e.g. in a char array). Use
pthread to create n threads to convert the string into a
complementary string (‘A’<->’Z’, ‘B’<->’Y’,
‘C’<->’X’, etc). You should divide this conversion task among
the n threads as evenly as possible. Print out the string
both before...
Hello I need help with this program. Should programmed in C!
Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...
The ExceptionLab class provided: – Creates an array of 100
elements and fills it with random numbers from 1 to 100. – It asks
the user for an index value between 0 and 99. – Prints the element
at that position. – If a number > 99 is entered by the user, the
class will abort with an ArrayIndexOutOfBoundsException • Modify
the ExceptionLab: – Add a try-catch clause which intercepts the
ArrayIndexOutOfBounds and prints the message: Index value cannot be...
Write a C program to assign natural numbers 1 to 100 into a one-dimensional integer array. Display all the values in the array on the screen. For each number in the array, determine if the number contains digit 7 or is divisible by 7. Display all those numbers on the screen. Original array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27...
1. Write a C or C++ program A6p2.c[pp] that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 39 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to convert all 60 array elements modulo 11 (i.e. take the remainder after division by 11) in place. You should divide this update task among the n threads as evenly as possible. Print the array both before and after...
Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...