hercioneto

QuickSort

Oct 2nd, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. //Rextester.Program.Main is the entry point for your code. Don't change it.
  2. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace Rextester
  10. {
  11.     public class Program
  12.     {
  13.         private static void quick( int[] v, int inicio, int fim )
  14.         {        
  15.         int aux, down, up, pivo;        
  16.         pivo = v[ inicio ];        
  17.         down = inicio;        
  18.         up   = fim;        
  19.         while( down < up ){  
  20.            
  21.             while( v[ down ] <= pivo && down < fim ) {
  22.              down++;
  23.             }
  24.            
  25.             while( v[ up ] > pivo    && up > inicio ) {
  26.              up--;
  27.             }
  28.            
  29.             if( down < up ){
  30.                  aux       = v[ down ];
  31.                  v[ down ] = v[ up ];
  32.                  v[ up ]   = aux;                
  33.             }
  34.         }
  35.         v[ inicio ] = v[ up ];
  36.         v[ up ] = pivo;
  37.             if( inicio < up && inicio != up-1 ) {
  38.                 quick( v , inicio , up-1 );
  39.             }
  40.             if( fim > down && up+1 != fim ) {
  41.                 quick( v , up+1 , fim );    
  42.             }
  43.         }
  44.  
  45.        
  46.         public static void Main(string[] args)
  47.         {
  48.             Console.WriteLine("Início!");
  49.             int[] vetor = new int[]{42,34,56,18,71,23,25,66,87};
  50.            
  51.             Console.WriteLine("\nVetor inicial:\n");
  52.             for (int r = 0; r < vetor.Length; r++)
  53.             {
  54.                 Console.Write("{0}  ", vetor[r]);
  55.             }
  56.            
  57.             quick( vetor , 0 , vetor.Length-1 );
  58.            
  59.             Console.WriteLine("\nVetor final:\n");
  60.             for (int r = 0; r < vetor.Length; r++)
  61.             {
  62.                 Console.Write("{0}  ", vetor[r]);
  63.             }
  64.  
  65.            
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment