Advertisement
Viksy

SortingAlgorithms

Nov 12th, 2022
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class SortingAlgorithms {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int[] nums = Arrays.stream(scanner.nextLine().split(" "))
  9.                 .mapToInt(Integer::parseInt)
  10.                 .toArray();
  11.  
  12.         int n = nums.length;
  13.  
  14.         //selectionSort(nums, n);
  15.         //bubbleSort(nums, n);
  16.         //insertionSort(nums, n);
  17.         //mergeSplit(nums,0, n - 1);
  18.         //quickSort(nums, 0, n - 1);
  19.  
  20.         System.out.println(Arrays.toString(Arrays.stream(nums).toArray()));
  21.     }
  22.  
  23.     public static void selectionSort(int[] nums, int n) {
  24.         for (int i = n - 1; i >= 1; i--) {
  25.             int maxIndex = i;
  26.  
  27.             for (int j = 0; j < i; j++) {
  28.                 if(nums[j] >= nums[maxIndex]) {
  29.                     maxIndex = j;
  30.                 }
  31.             }
  32.  
  33.             int temp = nums[i];
  34.             nums[i] = nums[maxIndex];
  35.             nums[maxIndex] = temp;
  36.         }
  37.     }
  38.  
  39.     public static void bubbleSort(int[] nums, int n) {
  40.         for (int i = n - 1; i >= 1; i--) {
  41.             for (int j = 1; j <= i; j++) {
  42.                 if(nums[j - 1] > nums[j]) {
  43.                     int temp = nums[j - 1];
  44.                     nums[j - 1] = nums[j];
  45.                     nums[j] = temp;
  46.                 }
  47.             }
  48.         }
  49.     }
  50.  
  51.     public static void insertionSort(int[] nums, int n) {
  52.         for (int i = 1; i < n; i++) {
  53.             int next = nums[i];
  54.             int j;
  55.  
  56.             for (j = i - 1; j >= 0 && nums[j] > next ; j--) {
  57.                 nums[j + 1] = nums[j];
  58.             }
  59.  
  60.             nums[j + 1] = next;
  61.         }
  62.     }
  63.  
  64.     public static void mergeSplit(int[] nums, int low, int high) {
  65.         if(low < high) {
  66.             int mid = (low + high) / 2;
  67.  
  68.             mergeSplit(nums, low, mid);
  69.             mergeSplit(nums, mid + 1, high);
  70.  
  71.             mergeSort(nums, low, mid, high);
  72.         }
  73.     }
  74.  
  75.     public static void mergeSort(int[] nums, int low, int mid, int high) {
  76.         int n = high - low + 1;
  77.         int[] temp = new int[n];
  78.         int left = low, right = mid + 1, tempindex = 0;
  79.  
  80.         while (left <= mid && right <= high) {
  81.             if(nums[left] <= nums[right]) {
  82.                 temp[tempindex++] = nums[left++];
  83.             } else {
  84.                 temp[tempindex++] = nums[right++];
  85.             }
  86.         }
  87.  
  88.         while (left <= mid) {
  89.             temp[tempindex++] = nums[left++];
  90.         }
  91.         while (right <= high) {
  92.             temp[tempindex++] = nums[right++];
  93.         }
  94.  
  95.         for (int i = 0; i < n; i++) {
  96.             nums[low + i] = temp[i];
  97.         }
  98.     }
  99.  
  100.     public static void quickSort(int[] nums, int low, int high) {
  101.         if (low < high) {
  102.             int partitionIndex = partition(nums, low, high);
  103.  
  104.             quickSort(nums, low, partitionIndex - 1);
  105.             quickSort(nums, partitionIndex + 1, high);
  106.         }
  107.     }
  108.  
  109.     private static int partition(int[] nums, int low, int high) {
  110.         int pivot = nums[high];
  111.         int index = (low - 1);
  112.  
  113.         for (int j = low; j <= high - 1; j++) {
  114.             if(nums[j] < pivot) {
  115.                 index++;
  116.  
  117.                 int temp = nums[index];
  118.                 nums[index] = nums[j];
  119.                 nums[j] = temp;
  120.             }
  121.         }
  122.  
  123.         index++;
  124.  
  125.         int temp = nums[index];
  126.         nums[index] = nums[high];
  127.         nums[high] = temp;
  128.  
  129.         return index;
  130.     }
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement