Advertisement
lashrone1

stooge

Sep 22nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.60 KB | None | 0 0
  1. import java.io.*;
  2. import java.io.File;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     static int counter=0;
  7.  
  8.     static void stoogesort(int arr[], int l, int h) {
  9.  
  10.         if (l >= h)
  11.             return;
  12.         counter++;
  13.  
  14.         if (arr[l] > arr[h]) {
  15.             int t = arr[l];
  16.             arr[l] = arr[h];
  17.             arr[h] = t;
  18.         }
  19.  
  20.  
  21.  
  22.         if (h - l + 1 > 2) {
  23.             int t = (h - l + 1) / 3;
  24.  
  25.             stoogesort(arr, l, h - t);
  26.  
  27.             stoogesort(arr, l + t, h);
  28.  
  29.             stoogesort(arr, l, h - t);
  30.  
  31.         }
  32.     }
  33.  
  34.     static void stoogesort_r(int arr[], int f, int h) {
  35.  
  36.         if (f >= h)
  37.             return;
  38.         counter++;
  39.         if (arr[f] < arr[h]) {
  40.             int t = arr[f];
  41.             arr[f] = arr[h];
  42.             arr[h] = t;
  43.  
  44.         }
  45.  
  46.  
  47.         if (h - f + 1 > 2) {
  48.             int t = (h - f + 1) / 3;
  49.  
  50.             stoogesort_r(arr, f, h - t);
  51.  
  52.             stoogesort_r(arr, f + t, h);
  53.  
  54.             stoogesort_r(arr, f, h - t);
  55.         }
  56.     }
  57.  
  58.     public static int[] readFiles(String file) {
  59.         try {
  60.             File f = new File(file);
  61.             Scanner s = new Scanner(f);
  62.             int ctr = 0;
  63.             while(s.hasNextInt()){
  64.                 ctr++;
  65.                 s.nextInt();
  66.             };
  67.             int[] arr = new int[ctr];
  68.             Scanner s1 = new Scanner(f);
  69.             for (int i = 0; i < arr.length; i++)
  70.                 arr[i] = s1.nextInt();
  71.             return arr;
  72.         } catch (Exception e) {
  73.             return null;
  74.         }
  75.     }
  76.  
  77.     static void nedostoogesort(int arr[], int l, int h){
  78.  
  79.  
  80.         if (l >= h)
  81.             return;
  82.         counter++;
  83.         if (arr[l] > arr[h]) {
  84.             int t = arr[l];
  85.             arr[l] = arr[h];
  86.             arr[h] = t;
  87.  
  88.         }
  89.  
  90.  
  91.         if (h - l + 1 > 2) {
  92.             int t = (h - l + 1) / 3;
  93.  
  94.             nedostoogesort(arr, l, h - t);
  95.  
  96.             nedostoogesort(arr, l + t, h);
  97.  
  98.             int f = arr[0];
  99.             arr[0] = arr[999];
  100.             arr[999] = f;
  101.  
  102.             nedostoogesort(arr, l, h - t);
  103.  
  104.         }
  105.  
  106.     }
  107.  
  108.  
  109.     public static void main(String args[]) throws IOException {
  110.  
  111.         int[] arr = readFiles("1000.txt");
  112.  
  113.         /////////////////////////////////
  114.         System.out.println("STOOGE");
  115.         long timeStart = System.currentTimeMillis();
  116.         stoogesort(arr,0,arr.length-1);
  117.         long timeSpend = System.currentTimeMillis() - timeStart;
  118.  
  119.         for (int i = 0; i < arr.length; i++) {
  120.             if (i % 10 == 0)
  121.                 System.out.println();
  122.             System.out.print(arr[i] + " ");
  123.         }
  124.         System.out.println();
  125.         System.out.println("Miliseconds: " + timeSpend+"\n"+counter);
  126.  
  127.         System.out.println();System.out.println();
  128.         /////////////////////////////////
  129.         arr = readFiles("1000.txt");
  130.         System.out.println("STOOGE REV");
  131.         timeSpend = 0;
  132.         stoogesort_r(arr,0,arr.length-1);
  133.         timeSpend = System.currentTimeMillis() - timeStart;
  134.         for (int i = 0; i < arr.length; i++) {
  135.             if (i % 10 == 0)
  136.                 System.out.println();
  137.             System.out.print(arr[i] + " ");
  138.         }
  139.         System.out.println();
  140.         System.out.println("Miliseconds: " + timeSpend+"\n"+counter);
  141.  
  142.         System.out.println();System.out.println();
  143.  
  144.         /////////////////////////////////
  145.         arr = readFiles("1000.txt");
  146.         System.out.println("STOOGE_100");
  147.         timeSpend = 0;
  148.  
  149.  
  150.         for(int i=0;i<arr.length;i+=100){
  151.             stoogesort(arr, i, i+99);
  152.         }
  153.         for (int i = 0; i < arr.length; i++) {
  154.             if (i % 10 == 0)
  155.                 System.out.println();
  156.             if (i % 100 == 0)
  157.                 System.out.println();
  158.             System.out.print(arr[i] + " ");
  159.         }
  160.         System.out.println();
  161.         timeSpend = System.currentTimeMillis() - timeStart;
  162.         System.out.println("Miliseconds: " + timeSpend+"\n"+counter);
  163.  
  164.         System.out.println();System.out.println();
  165.  
  166.         /////////////////////////////////
  167.         arr = readFiles("1000.txt");
  168.         System.out.println("NEDO STOOGE");
  169.         timeSpend = 0;
  170.         nedostoogesort(arr,0,arr.length-1);
  171.         timeSpend = System.currentTimeMillis() - timeStart;
  172.         for (int i = 0; i < arr.length; i++) {
  173.             if (i % 10 == 0)
  174.                 System.out.println();
  175.             System.out.print(arr[i] + " ");
  176.         }
  177.         System.out.println();
  178.         System.out.println("Miliseconds: " + timeSpend+"\n"+counter);
  179.  
  180.  
  181.  
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement