Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sorting
- {
- //Selection Sort
- //Separa entre sorted y unsorted, busca el minimo valor y lo pone en sorted
- public int[] SelectionSort(int[] allNums)
- {
- int smallest;
- for(int i = 0; i < allNums.Length-1; i++)
- {
- smallest = i; //indice, no numero
- for (int j = i+1; j <allNums.Length; j++)
- {
- if (allNums[j] < allNums[smallest]) smallest = j;
- }
- }
- int aux = allNums[i];
- allNums[i] = allNums[smallest];
- allNums[smallest] = aux;
- return allNums;
- }
- //Bubble Sort
- //Compara dos y chequea/ swappea
- public int[] BubbleSort(int[] allNums)
- {
- int temp;
- for(int i = 0; i<allNums.Length-1; i++)
- {
- for(int j= 0; j<allNums.Length-1; j++)
- {
- if (allNums[j] > allNums[j + 1])
- {
- int temp = allNums[j + 1];
- allNums[j + 1] = allNums[j];
- allNums[j] = temp;
- }
- }
- }
- return allNums;
- }
- //Insertion
- //Mas eficiente que bubble y selection
- //Empieza con el segundo numero y compara con los anteriores
- //Si es menor swappea con el menor
- public int[] Insertion(int[] nums)
- {
- int inner;
- int temp;
- for(int outer =1; outer < nums.Length; outer++)
- {
- temp = nums[outer];
- inner = outer;
- while(inner > 0 && nums[inner-1] >= temp)
- {
- nums[inner] = nums[inner - 1];
- inner -= 1;
- }
- nums[inner] = temp;
- }
- //Aca creo que falta algo
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement