Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to implement bubble sort on int array in C#

1 Answer

0 votes
using System;
 
class Program
{
    static void bubble_sort(int[] arr) {
        int tmp;
        
        for (int i = 0; i < arr.Length; i++) {
            for (int j = 0; j < arr.Length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    tmp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = tmp;
                }
            }
        }
    }
    
    static void Main() {
        int[] arr = new int[10];
             
        Random rnd = new Random();
        for (int i = 0; i < arr.Length; i++) {
             arr[i] = rnd.Next(1, 100);
        }

        bubble_sort(arr);
            
        foreach (int n in arr) {
            Console.Write("{0} ", n);
        }
    }
}

 
 
/*
run:

4 13 13 31 34 35 49 83 96 97 
  
*/


answered Apr 9, 2014 by avibootz
edited Jun 26, 2024 by avibootz

Related questions

1 answer 201 views
1 answer 162 views
1 answer 160 views
1 answer 99 views
1 answer 56 views
1 answer 69 views
1 answer 83 views
...