Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. simple explaination

simple explaination

Scheduled Pinned Locked Moved C#
comalgorithmsdata-structuresquestionlounge
5 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sacoskun
    wrote on last edited by
    #1

    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 -

    A 1 Reply Last reply
    0
    • S sacoskun

      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 -

      A Offline
      A Offline
      Atul Kale
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • A Atul Kale

        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.

        S Offline
        S Offline
        sacoskun
        wrote on last edited by
        #3

        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 -

        A 1 Reply Last reply
        0
        • S sacoskun

          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 -

          A Offline
          A Offline
          Atul Kale
          wrote on last edited by
          #4

          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.

          S 1 Reply Last reply
          0
          • A Atul Kale

            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.

            S Offline
            S Offline
            sacoskun
            wrote on last edited by
            #5

            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 -

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups