Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Sort List of Ages Using A Stack
/****************************************************************************************** PROBLEM: Sort An Array of Integers ******************************************************************************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static int[] list; public static void Main(string[] args) { //Load the list of integers list = new int[]{11, 2, 5, 6 , 10, 18, 13, 500, 1, 9, 91, 11, 500}; int[] stackArray = new int[list.Count()]; int stackTop = -1; foreach(int number in list) { if(stackTop == -1) { stackTop++; stackArray[stackTop]=number; } else { bool found = false; int targetIndex = -1; for(int s=0; s<stackTop+1 && !found; s++) { if(number <= stackArray[s]) { targetIndex = s; found = true; } } if(found) { //move all elements down for(int t=stackTop; t>=targetIndex; t--) { stackArray[t+1] = stackArray[t]; } //insert stackTop++; stackArray[targetIndex]=number; } else { //just insert stackTop++; stackArray[stackTop]=number; } } } for(int z=0; z <= stackTop; z++) { Console.WriteLine(stackArray[z]); } } } }
run
|
edit
|
history
|
help
0
Operator Overloading Example (+, -, *, /, ==, !=)
Strings Mix Complete
Delegaty
Select, Foreach and Exceptions in C#
Test
PrivateConstructorToCreateSingleObject
Permute
K
gyakorlas
CRM_Datum