simple explaination
-
Hi again, This code is for sorting the numbers which are given randomly in an array. private void BubbleSort(int[] a) { for ( int i = 0; i < a.Length - 1; i++ ) { for ( int j = 0; j < a.Length - i - 1; j++ ) { if ( a[j] > a[j + 1] ) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } } private void sort_Click(object sender, System.EventArgs e) { const int count = 1000; Random r = new Random(); int[] a = new int[count]; for ( int i = 0; i < count; i++ ) a[i] = r.Next(1000); //numbers 0 - 999 BubbleSort(a); for ( int i = 0; i < count; i++ ) { richTextBox1.AppendText( a[i].ToString() + " " ); } } } I want to ask why we used the second for loop can't we do it with only one "for loop"? And why we substracted i(int) in the second loop's condition? Kind Regards, - kromozom@msn.com for MSN Messenger -
-
Hi again, This code is for sorting the numbers which are given randomly in an array. private void BubbleSort(int[] a) { for ( int i = 0; i < a.Length - 1; i++ ) { for ( int j = 0; j < a.Length - i - 1; j++ ) { if ( a[j] > a[j + 1] ) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } } private void sort_Click(object sender, System.EventArgs e) { const int count = 1000; Random r = new Random(); int[] a = new int[count]; for ( int i = 0; i < count; i++ ) a[i] = r.Next(1000); //numbers 0 - 999 BubbleSort(a); for ( int i = 0; i < count; i++ ) { richTextBox1.AppendText( a[i].ToString() + " " ); } } } I want to ask why we used the second for loop can't we do it with only one "for loop"? And why we substracted i(int) in the second loop's condition? Kind Regards, - kromozom@msn.com for MSN Messenger -
Yes Pal, It's very much possible. Just see the code snippet.
private void Swap(int arr[], int pos1, int pos2) { int tmp = arr[pos1]; arr[pos1] = arr[pos2]; arr[pos2] = tmp; } public void BubbleSort(int arr[]) { int ctr = 0; while(ctr arr[ctr1+1]) { Swap(arr, ctr, ctr+1); ctr=0; continue; } ctr++; } }
U could use a for loop too if u want.
Hope this helps!:)
regards Atul Kale MCSD, MCT Sr. Software Engineer XcelVision Technologies Ltd. -
Yes Pal, It's very much possible. Just see the code snippet.
private void Swap(int arr[], int pos1, int pos2) { int tmp = arr[pos1]; arr[pos1] = arr[pos2]; arr[pos2] = tmp; } public void BubbleSort(int arr[]) { int ctr = 0; while(ctr arr[ctr1+1]) { Swap(arr, ctr, ctr+1); ctr=0; continue; } ctr++; } }
U could use a for loop too if u want.
Hope this helps!:)
regards Atul Kale MCSD, MCT Sr. Software Engineer XcelVision Technologies Ltd.oh, thanks buddy. This is another(better I think:)) way to do a sorting. However I couldn't understand the while loop there?while (ctr)? and thanks again:) If you wish you can add me into your MSN contact list(if you are using it:)) - kromozom@msn.com for MSN Messenger -
-
oh, thanks buddy. This is another(better I think:)) way to do a sorting. However I couldn't understand the while loop there?while (ctr)? and thanks again:) If you wish you can add me into your MSN contact list(if you are using it:)) - kromozom@msn.com for MSN Messenger -
Hey, I'm sorry, that code was a Copy Paste mistake. Since it was HTML it didn't understand the code in less than and Greater than signs. It took it as an HTML tag! Here's the rectified one.
private void Swap(int arr[], int pos1, int pos2) { int tmp = arr[pos1]; arr[pos1] = arr[pos2]; arr[pos2] = tmp; } public void BubbleSort(int arr[]) { int ctr = 0; while(ctr<arr.GetLength(0)-1) { if(arr[ctr1] > arr[ctr1+1]) { Swap(arr, ctr, ctr+1); ctr=0; continue; } ctr++; } }
Atul Kale MCSD, MCT Sr. Software Engineer XcelVision Technologies Ltd. -
Hey, I'm sorry, that code was a Copy Paste mistake. Since it was HTML it didn't understand the code in less than and Greater than signs. It took it as an HTML tag! Here's the rectified one.
private void Swap(int arr[], int pos1, int pos2) { int tmp = arr[pos1]; arr[pos1] = arr[pos2]; arr[pos2] = tmp; } public void BubbleSort(int arr[]) { int ctr = 0; while(ctr<arr.GetLength(0)-1) { if(arr[ctr1] > arr[ctr1+1]) { Swap(arr, ctr, ctr+1); ctr=0; continue; } ctr++; } }
Atul Kale MCSD, MCT Sr. Software Engineer XcelVision Technologies Ltd.oh, allright sometimes Copy&Paste can cause problems:). I understood the while loop here now. Thanks again for your help. - kromozom@msn.com for MSN Messenger -