Universitet jizzax filiali
Download 179.79 Kb.
|
Structura 3 modul
Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; namespace _8amaliy { class Program { static void Main(string[] args) { Console.WriteLine("Massiv kiriting: "); string s = Console.ReadLine(); string[] s1 = s.Split(' '); int[] son = new int[s1.Length]; int[] son1 = new int[s1.Length]; for (int i = 0; i < s1.Length; i++) { son[i] = Convert.ToInt32(s1[i]); } son1 = son; Stopwatch stopwatch = new Stopwatch(); for (int i = 0; i < 1000; i++) { stopwatch.Start(); HeapSort(son, 3); stopwatch.Stop(); son = son1; Console.WriteLine(stopwatch.ElapsedMilliseconds + "ms"); } Console.WriteLine("Tariblangan sonlar: "); foreach(int i in son) { Console.Write(i + " "); } Console.WriteLine(); Console.WriteLine("Tartiblash vaqti: "); Console.WriteLine(stopwatch.ElapsedMilliseconds + "ms"); Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds/1000 + "ms"); Console.ReadKey(); } static void HeapSort(int[] ar, int b) { int n = ar.Length; for (int i = n/2-1; i >= 0; i--) { Heap(ar, n, i, b); } for(int i=n-1; i>=0; i--) { int t = ar[0]; ar[0] = ar[i]; ar[i] = t; if (i < b) { b--; } Heap(ar, i, 0, b); } } static void Heap(int[] ar, int n, int i, int b) { int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; if(l { largest = l; } if (r < n && ar[r] > ar[largest]) { largest = r; } if (largest != i) { int s = ar[i]; ar[i] = ar[largest]; ar[largest] = s; if (largest < b) { Heap(ar, n, largest, b); } } } } } Download 179.79 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling