Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

bubble sort in c#

public static void Sort<T>(T[] array) where T : IComparable 
{
    for (int i = 0; i < array.Length; i++)
    {
        for (int j = 0; j < array.Length - 1; j++) 
        {
            if (array[j].CompareTo(array[j + 1]) > 0) 
            {
            	Swap(array, j, j + 1); 
            } 
        }
    }
}
private static void Swap<T>(T[] array, int first, int second) 
{
	T temp = array[first];
  	array[first] = array[second];
  	array[second] = temp;
}
Comment

c# bubble sort

// C# program for implementation 
// of Bubble Sort
using System;
  
class GFG
{ 
    static void bubbleSort(int []arr)
    {
        int n = arr.Length;
        for (int i = 0; i < n - 1; i++)
            for (int j = 0; j < n - i - 1; j++)
                if (arr[j] > arr[j + 1])
                {
                    // swap temp and arr[i]
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
    }
  
    /* Prints the array */
    static void printArray(int []arr)
    {
        int n = arr.Length;
        for (int i = 0; i < n; ++i)
            Console.Write(arr[i] + " ");
        Console.WriteLine();
    }
  
    // Driver method
    public static void Main()
    {
        int []arr = {64, 34, 25, 12, 22, 11, 90};
        bubbleSort(arr);
        Console.WriteLine("Sorted array");
        printArray(arr);
    }
  
}
  
// This code is contributed by Sam007
Comment

csharp bubble sort

using System;

public class Program {
   	//Bubble sort function
    private static float[] BubbleSort(float[] Array){
        bool Sorted = false;

        while (!Sorted){
            Sorted = true;
            for (int i = 0; i < Array.Length-1; i++)
            {
                if (Array[i] > Array[i+1]){
                    //swap Array[i] and Array[i+1]
                  	Sorted = false;
                    float Temp = Array[i+1];
                    Array[i+1] = Array[i];
                    Array[i] = Temp;
                }
            }
        }
        return Array;
    }
    public static void Main() {
        float[] UnSortedList = {1,56,3,6,7,543,542,1,555,4,1091};
        float[] SortedList = BubbleSort(UnSortedList);
		//prints the Sorted array
      	for (int i = 0; i < SortedList.Length; i++)
        {
            Console.Write(SortedList[i] + " ");
        }
        Console.ReadKey();
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: checkbox in c# 
Csharp :: properties in c# 
Csharp :: Get Mac address of Device in Xamarin 
Csharp :: foreach c# linq example 
Csharp :: c# .net automapper profile 
Csharp :: C# How to make a field read-only outside of class 
Csharp :: double parse csharp removes decimal 
Csharp :: unity scene switch 
Csharp :: kendo validator tries to validate hidden fields 
Csharp :: c# usermanager update user 
Csharp :: c# window form align right bottom 
Csharp :: how to make 3d field of view in unity 
Csharp :: how to make a block disappear in unity 
Csharp :: Send Hotmail/Outlook Email C# (Win/ASP.NET) 
Csharp :: unity reload active scene 
Csharp :: c# create a dummy class 
Csharp :: c sharp system pause equivelent 
Csharp :: request a pricipal permission 
Csharp :: c# loop through queue 
Csharp :: unity array c# 
Csharp :: unity hide mouse first person 
Csharp :: unity awake 
Csharp :: Get a list of distinct values in List 
Csharp :: c# datagridview cell align center 
Csharp :: C# Change color 
Csharp :: convert rgb to float 
Csharp :: c# subtract 24 hours form datetime 
Csharp :: c# delete item from list 
Csharp :: crud operation in asp.net 
Csharp :: C# long 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =