Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- ////////======================================================////////
- class MainClass
- {
- static int BiggerIndx(int[] V, int a, int b)
- {
- if (a > b) return a;
- else return b;
- }
- ////////surprizly swaps elements
- ////////------------------------------------------------------////////
- static void Swap(ref int a, ref int b)
- {
- int t = a;
- a = b;
- b = t;
- }
- ////////Heap (out reapair)
- ////////------------------------------------------------------////////
- static void repair(ref int[] V, int p, int top)
- {
- int l = 2 * p + 1;
- int r = 2 * p + 2;
- if (top <= l) return;
- if (top == l + 1) r = l;
- int imax = V[l] > V[r] ? l : r;
- if (V[imax] > V[p])
- Swap(ref V[imax], ref V[p]);
- repair(ref V, imax, top);
- }
- ////////------------------------------------------------------////////
- public static void Main (string[] args)
- {
- Random R = new Random();//Random
- int[] A = new int[10];//array
- ////////simple intialization and output//////
- ////////--------------------------------------------------////////
- for (int i = 0 ; i < 10 ; i++)
- A[i] = R.Next(0,10);
- for (int i = 0; i < 10; i++)
- Console.Write("{0} ", A[i]);
- Console.WriteLine();
- ////////creating a heap and output////////
- ////////--------------------------------------------------////////
- for (int i = (A.Length) / 2; i >= 0; i--)
- repair(ref A, i, A.Length);
- for (int i = 0; i < 10; i++)
- Console.Write("{0} ", A[i]);
- Console.WriteLine();
- ////////Heap sort and output////////
- ////////--------------------------------------------------////////
- for (int i = A.Length - 1; i > 0; i--)
- {
- Swap(ref A[0], ref A[i]);
- repair(ref A, 0, i);
- }
- for (int i = 0; i < 10; i++)
- Console.Write("{0} ", A[i]);
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement