Advertisement
KTVX94

Sorting

Aug 9th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. public class Sorting
  2. {
  3.     //Selection Sort
  4.     //Separa entre sorted y unsorted, busca el minimo valor y lo pone en sorted
  5.  
  6.     public int[] SelectionSort(int[] allNums)
  7.     {
  8.         int smallest;
  9.         for(int i = 0; i < allNums.Length-1; i++)
  10.         {
  11.             smallest = i; //indice, no numero
  12.             for (int j = i+1; j <allNums.Length; j++)
  13.             {
  14.                 if (allNums[j] < allNums[smallest]) smallest = j;
  15.             }
  16.         }
  17.  
  18.         int aux = allNums[i];
  19.         allNums[i] = allNums[smallest];
  20.         allNums[smallest] = aux;
  21.  
  22.         return allNums;
  23.     }
  24.  
  25.     //Bubble Sort
  26.     //Compara dos y chequea/ swappea
  27.  
  28.     public int[] BubbleSort(int[] allNums)
  29.     {
  30.         int temp;
  31.         for(int i = 0; i<allNums.Length-1; i++)
  32.         {
  33.             for(int j= 0; j<allNums.Length-1; j++)
  34.             {
  35.                 if (allNums[j] > allNums[j + 1])
  36.                 {
  37.                     int temp = allNums[j + 1];
  38.                     allNums[j + 1] = allNums[j];
  39.                     allNums[j] = temp;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         return allNums;
  45.     }
  46.  
  47.     //Insertion
  48.     //Mas eficiente que bubble y selection
  49.     //Empieza con el segundo numero y compara con los anteriores
  50.     //Si es menor swappea con el menor
  51.  
  52.     public int[] Insertion(int[] nums)
  53.     {
  54.         int inner;
  55.         int temp;
  56.  
  57.         for(int outer =1; outer < nums.Length; outer++)
  58.         {
  59.             temp = nums[outer];
  60.             inner = outer;
  61.             while(inner > 0 && nums[inner-1] >= temp)
  62.             {
  63.                 nums[inner] = nums[inner - 1];
  64.                 inner -= 1;
  65.             }
  66.             nums[inner] = temp;
  67.         }
  68.         //Aca creo que falta algo
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement