Skip to content

Commit

Permalink
Merge pull request fnplus#322 from Nimisha94/master
Browse files Browse the repository at this point in the history
Add bubble sort in C#
  • Loading branch information
jenistenxavier authored Oct 7, 2019
2 parents 07a27be + 654d69e commit 623f7a0
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Algorithms/Searching & Sorting/Bubble Sort/bubble_sort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;

public class BubbleSort
{

public int[] bubblesort(int[] array) {
while(true) {
int swap_count = 0;
for(int i=0; i<array.Length-1; i++) {
if(array[i]>array[i+1]) {
int swap = array[i];
array[i] = array[i+1];
array[i+1] = swap;
swap_count += 1;
}
}
if(swap_count == 0) {
break;
}
}
return array;
}

public static void Main(string[] args)
{
int[] arr = new int[6]{1, 6, 2, 8, 2, 3};
arr = new BubbleSort().bubblesort(arr);
Console.WriteLine("Sorted array is");
for(int i=0;i<arr.Length;i++) {
Console.Write(arr[i] + " ");
}
}
}

0 comments on commit 623f7a0

Please sign in to comment.