Skip to content
This repository was archived by the owner on Jul 20, 2023. It is now read-only.

Latest commit

 

History

History
60 lines (52 loc) · 1.27 KB

index.md

File metadata and controls

60 lines (52 loc) · 1.27 KB
extends section title description
_layouts.documentation
content
C#
C# Dev Snippets

C#

Shuffle an Array

Method to shuffle an array of strings in place.

private void Shuffle(string[] e)
{
	var rnd = new Random();
	string temp;
	for (int i = e.Length - 1; i > 0; i--)
	{
		int j = rnd.Next(0, i);
		temp = e[i];
		e[i] = e[j];
		e[j] = temp;
	}
}

C#

Bubble Sort

Method to sort an arry using bubble sort

 public static void Main(string[] args)
         { 
            int[] a = { 3, 0, 2, 5, -1, 4, 1 }; 
			int t; 
			Console.WriteLine("Original array :");
            foreach (int aa in a)                       
            Console.Write(aa + " ");                     
            for (int p = 0; p <= a.Length - 2; p++)
            {
                for (int i = 0; i <= a.Length - 2; i++)
                {
                    if (a[i] > a[i + 1])
                    {
                        t = a[i + 1];
                        a[i + 1] = a[i];
                        a[i] = t;
                    }
                } 
            }
            Console.WriteLine("\n"+"Sorted array :");
            foreach (int aa in a)                       
            Console.Write(aa + " ");
			Console.Write("\n"); 
            
        }