Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MergeSort
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Your code goes here Console.WriteLine("Hello, world!"); int[] inputArray=new int[]{3,4,7,6,1,8}; int[] result=MergeSort(inputArray); for(int i=0;i<result.Length;i++) { Console.WriteLine(result[i]); } } private static int[] MergeSort(int[] inputArray) { if(inputArray.Length==1) return inputArray; int mid=inputArray.Length/2; int[] left= new int[mid]; for(int i=0;i<mid;i++) { left[i]=inputArray[i]; } int[] right= new int[inputArray.Length-mid]; int startIndex=0; for(int i=mid;i<inputArray.Length;i++) { right[startIndex]=inputArray[i]; startIndex++; } left=MergeSort(left); right=MergeSort(right); return merge(left,right); } private static int[] merge(int[] left,int[]right) { int[] result=new int[left.Length+right.Length]; int resultIndex=0; int leftIndex=0; int rightIndex=0; while(leftIndex<left.Length||rightIndex<right.Length) { if (leftIndex == left.Length) { result[resultIndex] = right[rightIndex]; rightIndex++; resultIndex++; } else if (rightIndex == right.Length) { result[resultIndex] = left[leftIndex]; leftIndex++; resultIndex++; } else { if (left[leftIndex] < right[rightIndex]) { result[resultIndex] = left[leftIndex]; leftIndex++; resultIndex++; } else if (left[leftIndex] >= right[rightIndex]) { result[resultIndex] = right[rightIndex]; rightIndex++; resultIndex++; } } } return result; } } }
run
|
edit
|
history
|
help
1
sdsadxaxsd
Main
SierpinskiTriangleChaosGameRecursion
Changing asp net user password
Root
Wild West of Meming
Find the integers which squares sum gives the prime numbers of the form (4k + 1).
Code wall rules
1
parsowanie daty