Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Sort A List of Ages of Students (Solution: Stack / O(N^3) Complexity)
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Problem: Sort a list of ages of all the students //Solution: Use a stack to arrange the order of the ages using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public class Student { public string Name { get; set; } public int Age {get; set;} } public static List<Student> StudentList; public static void Main(string[] args) { //Prepare the list of students LoadStudents(); //Create a stack int[] sortedAgeStack = new int[StudentList.Count()]; int stackTop = -1; foreach ( Student s in StudentList) { int currentValue = s.Age; //If stack is empty, just insert the age if(stackTop == -1) { stackTop++; sortedAgeStack[stackTop]=currentValue; } else { int insertIndex = -1; //find the location to insert by going through all the values in the stack for(int i=0;i<=stackTop;i++) { if(i==0 && currentValue <= sortedAgeStack[i]) { insertIndex = i; } else if(currentValue <= sortedAgeStack[i] && currentValue >= sortedAgeStack[i-1]) { insertIndex = i; } } if(insertIndex != -1) { //move the elements first for(int r=stackTop;r>=insertIndex;r--) { sortedAgeStack[r+1] = sortedAgeStack[r]; } //insert on insertIndex stackTop++; sortedAgeStack[insertIndex]=currentValue; } else { //just insert after the last element stackTop++; sortedAgeStack[stackTop] = currentValue; } } } //Print the sorted list of ages PrintResult(sortedAgeStack); } public static void PrintResult(int[] output) { foreach(int i in output) Console.WriteLine(i); } public static void LoadStudents() { Student s = new Student(){ Name = "Ivy", Age = 33}; Student s1 = new Student(){ Name = "Ryan", Age = 34}; Student s2 = new Student(){ Name = "Ron", Age = 23}; Student s3 = new Student(){ Name = "Melvin", Age = 2}; Student s4 = new Student(){ Name = "Sara", Age = 33}; Student s5 = new Student(){ Name = "Mars", Age = 43}; Student s6 = new Student(){ Name = "Pluto", Age = 50}; Student s7 = new Student(){ Name = "Neptune", Age = 6}; Student s8 = new Student(){ Name = "Nero", Age = 5}; Student s9 = new Student(){ Name = "Augustus", Age = 34}; StudentList = new List<Student>(); StudentList.Add(s); StudentList.Add(s1); StudentList.Add(s2); StudentList.Add(s3); StudentList.Add(s4); StudentList.Add(s5); StudentList.Add(s6); StudentList.Add(s7); StudentList.Add(s8); StudentList.Add(s9); } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0,16 sec, absolute running time: 0,09 sec, cpu time: 0,06 sec, average memory usage: 14 Mb, average nr of threads: 3
edit mode
|
history
|
discussion
2 5 6 23 33 33 34 34 43 50