C# example - bubble sort algorithm


Bubble sort algorithm

This is C# example code for a bubble sort algorithm. The code sorts an array of five integer items in ascendingorder. If you are not sure about the process of bubble sort, you can visit this page:

Algorithm and Data Structure tutorial  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)

{
   int[] arr= new int[5]{23,2,3,34,6}; // unsorted data set
   bubblesort(arr,5); // sorting process using bubble sort
   int i;
  for(i=0;i<5;i++)
      Console.Write(arr[i]+"\t"); //after sorting
  Console.ReadLine();

}
/// bubble sort

static void bubblesort(int[] dataset, int n){
   int i,j;
   for(i=0;i<n;i++)
     for(j=n-1;j>i;j--)
        if(dataset[j]<dataset[j-1])
       {
          int temp=dataset[j];
          dataset[j]=dataset[j-1];
          dataset[j-1]=temp;
        }

     }
  
}
}

c# bubble sort




Comments

פופה פופה פה comment

 פופה פופה פה

איך כולם פה רוקדים כמו בריו
פופה פופה פה


2016-05-01
Kaprov comment

 Kaprov

Thank you guys. This really helped me out.
Breclav, Czech Republic


2016-02-09
CAPTCHA image



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.