/************************************************ Grundlegende Algorithmen mit Java, http://algorithmen-und-problemloesungen.de/ Copyright @2007-2009 by Doina Logofatu in C#: Michael Gärtner ************************************************/ using System; using System.Collections.Generic; using System.IO; namespace Logofatu { class P04QSort { private static String FileInputName = "QSort.in"; private static String FileOutputName = "QSort.out"; private static void quickSort(int[] arr, int inf, int sup) { if ( inf < sup ) { int pivot = arr[inf]; int aux; int i = inf + 1; int j = sup; while ( i <= j ) { while ( i <= sup && arr[i] <= pivot ) i++; while ( j >= inf && arr[j] > pivot ) j--; if ( i < j && i <= sup && j >= inf ) { aux = arr[i]; arr[i] = arr[j]; arr[j] = aux; i++; j--; } } i--; arr[inf] = arr[i]; arr[i] = pivot; quickSort(arr, inf, i - 1); quickSort(arr, i + 1, sup); } } static void Main(string[] args) { StreamReader sr = null; StreamWriter sw = null; List list = new List(); try { sr = new StreamReader(FileInputName); sw = new StreamWriter(FileOutputName); while ( !sr.EndOfStream ) { string[] sArray = sr.ReadLine().Split(' '); for ( int i = 0; i < sArray.Length; i++ ) list.Add(int.Parse(sArray[i])); } int idx = list.Count; int[] arr = new int[idx]; for ( int i = 0; i < idx; i++ ) arr[i] = list[i]; quickSort(arr, 0, idx - 1); for ( int i = 0; i < idx; i++ ) { sw.Write(arr[i]); sw.Write(' '); } } catch ( IOException ex ) { Console.Write("Fehler bei der Dateiverarbeitung!\n" + ex); } finally { if ( sr != null ) { sr.Close(); } if ( sw != null ) { sw.Close(); } } } } }