الگوریتم مرتب سازی سریع (Quick Sort) در سی شارپ

ساخت وبلاگ

یکی از انواع مرتب سازی ، مرتب سازی سریع یا QuickSort می باشد . در سورس کد زیر شما را با این نوع مرتب سازی آشنا می کنیم .


تصویر زیر نشان دهنده روش کار الگوریتم مرتب سازی در سی شارپ می باشد :



using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Quicksort
{ class Program { static void Main(string[] args) { // Create an unsorted array of string elements string[] unsorted = { "z", "e", "x", "c", "m", "q", "a" };  // Print the unsorted array for (int i = 0; i < unsorted.Length; i++) { Console.Write(unsorted[i] + " "); } Console.WriteLine(); // Sort the array Quicksort(unsorted, 0, unsorted.Length - 1); // Print the sorted array for (int i = 0; i < unsorted.Length; i++) { Console.Write(unsorted[i] + " "); } Console.WriteLine(); Console.ReadLine(); } public static void Quicksort(IComparable[] elements, int left, int right) { int i = left, j = right; IComparable pivot = elements[(left + right) / 2]; while (i <= j) { while (elements[i].CompareTo(pivot) < 0) { i++; } while (elements[j].CompareTo(pivot) > 0) { j--; } if (i <= j) { // Swap IComparable tmp = elements[i]; elements[i] = elements[j]; elements[j] = tmp; i++; j--; } }  // Recursive calls if (left < j) { Quicksort(elements, left, j); } if (i < right) { Quicksort(elements, i, right); } } }
}


بدست آوردن سریال CPU با سی شارپ...
ما را در سایت بدست آوردن سریال CPU با سی شارپ دنبال می کنید

برچسب : الگوریتم, نویسنده : 6cristiansoft7 بازدید : 105 تاريخ : سه شنبه 7 آذر 1396 ساعت: 15:29