Я работаю через сортировки в C# и сейчас пишу быструю сортировку, и, кажется, синтаксис неверен.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SortQuick
{
class quickSortAlgorithm
{
private int[] array = new int[10];
private int len;
public static void quicksort(int left, int right)
{
int pivot;
pivot = array[left];
while(left < right)
{
while (array[left] <= 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--;
}
}
array[left] = pivot;
pivot = left;
if(left<pivot)
{
quicksort(left, pivot);
}
if(right>pivot)
{
quicksort(pivot+1, right);
}
}
static void Main(string[] args)
{
quickSortAlgorithm qSort = new quickSortAlgorithm();
int[] array = { 32, 43, 54, 65, 23, 56, 34, 78, 98, 12};
qSort.array = array;
qSort.len = qSort.array.Length;
quicksort(0, qSort.len - 1);
for (int j = 0; j < qSort.len;++j) {
Console.WriteLine(qSort.array[j]);
}
}
}
}
Компилятор недоволен массивом внутри функции, а также условием escape в l oop в main. Я написал это сам без конструктора, но ошибки, которые я получаю, - это то, что я могу получить на go, я просто не понимаю разницу в синтаксисе.
/*
* C# Program to Implement Quick Sort
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sortQuickAlgorithm
{
class quickSortAlgorithm
{
private int[] array = new int[20];
private int len;
public void QuickSortAlgorithm()
{
sort(0, len - 1);
}
public void sort(int left, int right)
{
int pivot, leftend, rightend;
leftend = left;
rightend = right;
pivot = array[left];
while (left < right)
{
while ((array[right] >= pivot) & amp; & (left < right))
{
right--;
}
if (left != right)
{
array[left] = array[right];
left++;
}
while ((array[left] >= pivot) & amp; & (left < right))
{
left++;
}
if (left != right)
{
array[right] = array[left];
right--;
}
}
array[left] = pivot;
pivot = left;
left = leftend;
right = rightend;
if (left < pivot) { sort(left, pivot - 1); }
if (right > pivot)
{
sort(pivot + 1, right);
}
}
public static void Main()
{
quickSortAlgorithm q_Sort = new quickSortAlgorithm();
int[] array = { 41, 32, 15, 45, 63, 72, 57, 43, 32, 52, 183 };
q_Sort.array = array;
q_Sort.len = q_Sort.array.Length;
q_Sort.QuickSortAlgorithm();
for (int j = 0; j & lt; q_Sort.len; j++)
{
Console.WriteLine(q_Sort.array[j]);
}
Console.ReadKey();
}
}
}
Это сообщение об ошибке:
Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field,
method, or property 'quickSortAlgorithm.array' SortQuick
C:\Program.cs 15 Active
Severity Code Description Project File Line Suppression State
Error CS0103 The name 'j' does not exist in the current context SortQuick
C:\SortQuick\SortQuick\Program.cs 55 Active
Error CS1026 ) expected SortQuick C:\SortQuick\SortQuick\Program.cs
55 Active
Error CS1002 ; expected SortQuick C:\SortQuick\SortQuick\Program.cs
55 Active
Error CS1513 } expected SortQuick C:\SortQuick\SortQuick\Program.cs
55 Active
Error CS1026 ) expected Miscellaneous Files C:\C#\Practice\Class1.cs
32 Active
Error CS1002 ; expected Miscellaneous Files C:\C#\Practice\Class1.cs
32 Active
Error CS1513 } expected Miscellaneous Files C:\C#\Practice\Class1.cs
32 Active
Error CS1026 ) expected Miscellaneous Files C:\C#\Practice\Class1.cs
43 Active
Error CS1002 ; expected Miscellaneous Files C:\C#\Practice\Class1.cs
43 Active
Error CS1513 } expected Miscellaneous Files C:\C#\Practice\Class1.cs
43 Active
Error CS1026 ) expected Miscellaneous Files C:\C#\Practice\Class1.cs
76 Active
Error CS1002 ; expected Miscellaneous Files C:\C#\Practice\Class1.cs
76 Active
Error CS1513 } expected Miscellaneous Files C:\C#\Practice\Class1.cs
76 Active
Error CS0103 The name 'lt' does not exist in the current context SortQuick
C:\SortQuick\SortQuick\Program.cs 55 Active
Error CS0201 Only assignment, call, increment, decrement, await, and new
object expressions can be used as a statement SortQuick
C:\Program.cs 55 Active
Error CS0103 The name 'j' does not exist in the current context SortQuick
C:\Program.cs 56 Active
Message IDE1006 Naming rule violation: These words must begin with upper
case
characters: quickSortAlgorithm SortQuick C:\Program.cs 8 Active
Message IDE1006 Naming rule violation: These words must begin with upper
case
characters: quicksort SortQuick C:\Program.cs 12 Active