Ich habe das nicht laufen, so dass es nicht ohne Zwicken funktionieren könnte, aber wenn es funktioniert Ich hoffe, Sie versuchen, herauszufinden, was mit Ihrer Version falsch war:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sortQuick
{
class quickSort
{
private double[] array = new double[1010];
private int len;
public void QuickSort()
{
sort(0, len - 1);
}
public void sort(int left, int right)
{
int leftend, rightend;
double pivot;
leftend = left;
rightend = right;
pivot = array[left];
while (left < right)
{
while ((array[right] >= pivot) && (left < right))
{
right--;
}
if (left != right)
{
array[left] = array[right];
left++;
}
while ((array[left] <= pivot) && (left < right))
{
left++;
}
if (left != right)
{
array[right] = array[left];
right--;
}
}
left = leftend;
right = rightend;
if (left < pivot)
{
sort(left, pivot - 1);
}
if (right > pivot)
{
sort(pivot + 1, right);
}
}
public static void Main()
{
quickSort q_Sort = new quickSort();
double[] years = System.IO.File.ReadAllLines(@"C:\WS1_Rain.txt");
IEnumerable<double> yearArray = years.Select(item => double.Parse(item));
double[] array = yearArray.ToArray();
q_Sort.array = array;
q_Sort.len = q_Sort.array.Length;
q_Sort.QuickSort();
for (int j = 0; j < q_Sort.len; j++)
{
Console.WriteLine(q_Sort.array[j]);
}
Console.ReadKey();
}
}
}
UPDATE: Ok , Ihr Code war schwer zu modifyso Ich habe den Originalcode von here und modifiziert, um mit Doppel zu arbeiten. Also hier ist eine funktionierende Version (ich habe es getestet).
class Program
{
static public int Partition(double[] numbers, int left, int right)
{
double pivot = numbers[left];
while (true)
{
while (numbers[left] < pivot)
left++;
while (numbers[right] > pivot)
right--;
if (left < right)
{
double temp = numbers[right];
numbers[right] = numbers[left];
numbers[left] = temp;
}
else
{
return right;
}
}
}
static public void QuickSort_Recursive(double[] arr, int left, int right)
{
// For Recusrion
if (left < right)
{
int pivot = Partition(arr, left, right);
if (pivot > 1)
QuickSort_Recursive(arr, left, pivot - 1);
if (pivot + 1 < right)
QuickSort_Recursive(arr, pivot + 1, right);
}
}
static void Main(string[] args)
{
double[] numbers = System.IO.File.ReadAllLines(@"C:\WS1_Rain.txt").Select(p => double.Parse(p)).ToArray();
int len = 9;
Console.WriteLine("QuickSort By Recursive Method");
QuickSort_Recursive(numbers, 0, len - 1);
for (int i = 0; i < 9; i++)
Console.WriteLine(numbers[i]);
Console.WriteLine();
Console.ReadKey();
}
Sie könnten in _Array.Sort_ verwenden? Gibt es einen Grund, eine eigene Sortierung zu schreiben? –
Kannst du (in deiner Frage) beschreiben, welche Art von Fehlern du bekommst oder wie die Reihenfolge der Zahlen einige Beispieleingaben enthält? Zum Beispiel, wenn Sie es 11.5, 12.2 und 13.3 geben, wie sortiert es sie? Das würde der Community helfen, Ihr Problem zu finden. –
"Will nicht mehr laufen"? Es lehnt einfach ab? Oder gibt es Ihnen einen Ausnahme- oder Compilerfehler? Wenn letzteres, posten Sie bitte diesen Fehler. –