Friday 15 January 2010

bubble sort in c# with array that user can input the numbers -



bubble sort in c# with array that user can input the numbers -

i want create user come in numbers , sorted bubble sorting didn't reach right implementation :/

class bubblesort { static void main(string[] args) { int[] = new int[5]; int t; (int p = 0; p <= a.length - 2; p++) { (int = 0; <= a.length - 2; i++) { if (a[i] > a[i + 1]) { t = a[i + 1]; a[i + 1] = a[i]; a[i] = t; } inputstudent(a[i]); } } console.writeline("the sorted array"); foreach (int aa in a) { console.write(aa + " "); } console.read(); } static void inputstudent(int p) { console.writeline("enter number"); p = int.parse(console.readline()); console.writeline(); } public static int { get; set; } }

you should numbers user first, can sort them. right inputstudent function isn't doing - takes in integer parameter, re-assigns value user, , exits.

you instead array of ints user:

private static int[] getintarrayfromuser(int numberofelementstoget) { var intarrayfromuser = new int[numberofelementstoget]; (int = 0; < numberofelementstoget; i++) { while (true) { // prompt user integer , response console.write("enter integer item #{0}: ", + 1); var input = console.readline(); // check response int.tryparse. if it's valid int, // assign current array index , break while loop int tmpint; if (int.tryparse(input, out tmpint)) { intarrayfromuser[i] = tmpint; break; } // if here, didn't break while loop, input invalid. console.writeline("{0} not valid integer. seek again.", input); } } homecoming intarrayfromuser; }

then can phone call method main populate array:

static void main(string[] args) { int[] = getintarrayfromuser(5); // five-element array // insert bubble-sort code here // show sorted array console.writeline("the sorted array:"); console.writeline(string.join(" ", a)); console.read(); }

c# arrays algorithm sorting user

No comments:

Post a Comment