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

bubble dort c#

int[] arr = { 800, 11, 50, 771, 649, 770, 240, 9 };

int temp = 0;

for (int write = 0; write < arr.Length; write++) {
    for (int sort = 0; sort < arr.Length - 1; sort++) {
        if (arr[sort] > arr[sort + 1]) {
            temp = arr[sort + 1];
            arr[sort + 1] = arr[sort];
            arr[sort] = temp;
        }
    }
}

for (int i = 0; i < arr.Length; i++)
    Console.Write(arr[i] + " ");

Console.ReadKey();
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 :: unity 2d 
Csharp :: generate certificate in windows 
Csharp :: override gethashcode 
Csharp :: how to get type of an object in c# 
Csharp :: C# redirecttoaction with area 
Csharp :: datetime empty date 
Csharp :: an existing connection was forcibly closed by the remote host. .net core 
Csharp :: c# merge two xml files 
Csharp :: c# round number up 
Csharp :: c# new list of objects 
Csharp :: unity camera follow with lerp 
Csharp :: linq sum 
Csharp :: system.net.mail send html message 
Csharp :: how to get row index of selected row in gridview asp.net webforms 
Csharp :: get unique array based on value in c# 
Csharp :: linq when name then orderby 
Csharp :: append 2 arrays c# 
Csharp :: how to minimum text length in textbox in c# 
Csharp :: mvc session key exists 
Csharp :: how to evaluate code in c# 
Csharp :: set current date to textbox in asp.net 
Csharp :: c# create list of objects 
Csharp :: how to find player gameobject in unity 
Csharp :: c# ternary operator 
Csharp :: regex for accepting a file name c# 
Csharp :: why is called c# 
Csharp :: how to keep rigidbody2D upright unity 
Csharp :: get quaternion from vector unity 
Csharp :: wpf datagrid get selected items 
Csharp :: viewBag as a list 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =