Java选择排序和插入排序

时间:2015-03-24 00:58:49

标签: java

//Justin Simpson
//CSC-251-FON01, Project 2
//Problem# 7, Page# 1003
//March 22, 2015


import java.util.Random;

/** 
 * This program uses sorting algorithms to take a array that is randomly generated arrays of 50 integers.
 * All four arrays are identical to the original array. Bubble sort, Insertions sort, selection  sort, and
 * Quick sort will be used to place the arrays in Ascending order.
 */


public class JustinSorting
{
   public static int count = 0;
   public static int count2 = 0;
   public static int size = 50;   //Holds the size of the arrays.

   public static void main(String[] args)
   {
       /**
        * Randomly creates the array then copies the same array to 4 other arrays. 
        */
       int[] original = new int[size];
       Random rand = new Random();
       for(int i = 0; i < size; i++)
       {
           original[i] = rand.nextInt(1000 - 100)+ 100;
       }
      int[] array1 = new int[size];
      int[] array2 = new int[size];
      int[] array3 = new int[size];
      int[] array4 = new int[size];

      System.arraycopy(original, 0, array1, 0, original.length);
      System.arraycopy(original, 0, array2, 0, original.length);
      System.arraycopy(original, 0, array3, 0, original.length);
      System.arraycopy(original, 0, array4, 0, original.length);

      //Calss each one of the sorting methods.
      bubbleSort(array1);
      insertionSort(array2);
      selectionSort(array3);
      quickSort(array4);

   }

  /**
   * Uses the bubble sort algorithm to sort the array.
   * @param array will pass the copied array.
   */
   public static void bubbleSort(int[] array)
   {  
      System.out.print("Bubble Sort:");
      System.out.print("\n-----------------------------------------");


      // Display the array's contents.
      System.out.println("\n|Original order:                        |");
      printArray(array);

      int lastPos;     // Position of last element to compare
      int index;       // Index of an element to compare
      int temp;        // Used to swap to elements

         // The outer loop positions lastPos at the last element
         // to compare during each pass through the array. Initially
         // lastPos is the index of the last element in the array.
         // During each iteration, it is decreased by one.
         for (lastPos = array.length - 1; lastPos >= 0; lastPos--)
         {
            // The inner loop steps through the array, comparing
            // each element with its neighbor. All of the elements
            // from index 0 thrugh lastPos are involved in the
            // comparison. If two elements are out of order, they
            // are swapped.
            for (index = 0; index <= lastPos - 1; index++)
            {
               // Compare an element with its neighbor.
               if (array[index] > array[index + 1])
               {
                   count++;
                  // Swap the two elements.
                  temp = array[index];
                  array[index] = array[index + 1];
                  array[index + 1] = temp;
               }
               count2++;
            }

         }
      // Display the array's contents.
      System.out.print("|                                       |\n");
      System.out.println("|Sorted order:                          |");
      printArray(array);
      System.out.print("|                                       |\n");         
      System.out.print("|Swaps:" + count + "                              |");
      System.out.print("\n|Comparisons:" + count2 + "                       |");
      System.out.print("\n-----------------------------------------\n");

      count = 0;
      count2 = 0;

   }


   /**
    * Uses the insertion sort algorithm to sort the array.
    * @param array will pass the copied array.
    */
   public static void insertionSort(int[] array)
   {
      int unsortedValue;  // The first unsorted value
      int scan, scan1;    // Used to scan the array

      System.out.print("Insertion Sort: ");
      System.out.print("\n-----------------------------------------");

      // Display the array's contents.
      System.out.println("\n|Original order:                        |");
      printArray(array);

      // The outer loop steps the index variable through 
      // each subscript in the array, starting at 1. The portion of
      // the array containing element 0  by itself is already sorted.
      for (int index = 1; index < array.length; index++)
      {
         // The first element outside the sorted portion is
         // array[index]. Store the value of this element
         // in unsortedValue.
         unsortedValue = array[index];

         // Start scan at the subscript of the first element
         // outside the sorted part.
         scan = index;
         scan1 = index;

         // Move the first element in the still unsorted part
         // into its proper position within the sorted part.
         while (scan > 0 && array[scan-1] > unsortedValue)
         {
            array[scan] = array[scan - 1];
            scan--;
            count++;
            count2++;
         }
         count2++;

         // Insert the unsorted value in its proper position
         // within the sorted subset.
         array[scan] = unsortedValue;
      }
      // Display the array's contents.
      System.out.print("|                                       |\n");
      System.out.println("|Sorted order:                          |");
      printArray(array);
      System.out.print("|                                       |\n");         
      System.out.print("|Swaps:" + count + "                              |");
      System.out.print("\n|Comparisons:" + count2 + "                        |");
      System.out.print("\n-----------------------------------------\n");

      count = 0;
      count2 = 0;


   }

   /**
    * Uses the selection sort algorithm to sort the array.
    * @param array will pass the copied array.
    */
   public static void selectionSort(int[] array)
   {
      int startScan;   // Starting position of the scan
      int index;       // To hold a subscript value
      int minIndex;    // Element with smallest value in the scan
      int minValue;    // The smallest value found in the scan

      System.out.print("Selection Sort: ");
      System.out.print("\n-----------------------------------------");

      // Display the array's contents.
      System.out.println("\n|Original order:                        |");
      printArray(array);


      // The outer loop iterates once for each element in the
      // array. The startScan variable marks the position where
      // the scan should begin.
      for (startScan = 0; startScan < (array.length-1); startScan++)
      {
         count++;
         // Assume the first element in the scannable area
         // is the smallest value.
         minIndex = startScan;
         minValue = array[startScan];

         // Scan the array, starting at the 2nd element in
         // the scannable area. We are looking for the smallest
         // value in the scannable area. 
         for(index = startScan + 1; index < array.length; index++)
         {
            if (array[index] < minValue || array[index] < minValue)
            {
               count2++;
            }
            if (array[index] < minValue)
            {
               minValue = array[index];
               minIndex = index;
               count2++;
            }
         }

         // Swap the element with the smallest value 
         // with the first element in the scannable area.
         array[minIndex] = array[startScan];
         array[startScan] = minValue;
      }
      // Display the array's contents.
      // Display the array's contents.
      System.out.print("|                                       |\n");
      System.out.println("|Sorted order:                          |");
      printArray(array);
      System.out.print("|                                       |\n");         
      System.out.print("|Swaps:" + count + "                               |");
      System.out.print("\n|Comparisons:" + count2 + "                        |");
      System.out.print("\n-----------------------------------------\n");


      count = 0;
      count2 = 0;
   }

   /**
    * Uses the quick sort algorithm to sort the array.
    * @param array will pass the copied array.
    */
   public static void quickSort(int array[])
   {

      System.out.print("Quick Sort: ");
      System.out.print("\n-----------------------------------------");


      // Display the array's contents.
      System.out.println("\n|Original order:                        |");

      printArray(array);

      doQuickSort(array, 0, array.length - 1);

      // Display the array's contents.
      System.out.print("|                                       |\n");
      System.out.println("|Sorted order:                          |");
      printArray(array);
      System.out.print("|                                       |\n");         
      System.out.print("|Swaps:" + count + "                              |");
      System.out.print("\n|Comparisons:" + count2 + "                        |");
      System.out.print("\n-----------------------------------------\n");

   }

   /**
      The doQuickSort method uses the QuickSort algorithm
      to sort an int array.
      @param array The array to sort.
      @param start The starting subscript of the list to sort
      @param end The ending subscript of the list to sort
   */

   public static void doQuickSort(int array[], int start, int end)
   {
      int pivotPoint;

      if (start < end)
      {
         // Get the pivot point.
         pivotPoint = partition(array, start, end);

         // Sort the first sub list.
         doQuickSort(array, start, pivotPoint - 1);

         // Sort the second sub list.
         doQuickSort(array, pivotPoint + 1, end);
      }
   }

   /**
      The partiton method selects a pivot value in an array
      and arranges the array into two sub lists. All the
      values less than the pivot will be stored in the left
      sub list and all the values greater than or equal to
      the pivot will be stored in the right sub list.
      @param array The array to partition.
      @param start The starting subscript of the area to partition.
      @param end The ending subscript of the area to partition.
      @return The subscript of the pivot value.
   */

   public static int partition(int array[], int start, int end)
   {
      int pivotValue;    // To hold the pivot value
      int endOfLeftList; // Last element in the left sub list.
      int mid;           // To hold the mid-point subscript

      // Find the subscript of the middle element.
      // This will be our pivot value.
      mid = (start + end) / 2;

      // Swap the middle element with the first element.
      // This moves the pivot value to the start of 
      // the list.
      swap(array, start, mid);

      // Save the pivot value for comparisons.
      pivotValue = array[start];

      // For now, the end of the left sub list is
      // the first element.
      endOfLeftList = start;

      // Scan the entire list and move any values that
      // are less than the pivot value to the left
      // sub list.
      for (int scan = start + 1; scan <= end; scan++)
      {
         if (array[scan] < pivotValue)
         {
            count++;
            endOfLeftList++;
            swap(array, endOfLeftList, scan);
         }
         count2++;
      }

      // Move the pivot value to end of the
      // left sub list.
      swap(array, start, endOfLeftList);

      // Return the subscript of the pivot value.
      return endOfLeftList;
   }

   /**
      The swap method swaps the contents of two elements
      in an int array.
      @param The array containing the two elements.
      @param a The subscript of the first element.
      @param b The subscript of the second element.
   */

   public static void swap(int[] array, int a, int b)
   {
      int temp;

      temp = array[a];
      array[a] = array[b];
      array[b] = temp;
   }

   public static void printArray(int[] array)
   {
      int index = 0;
      int index2 = 1;
      for (int element : array)
      {
         index++;
         if (index == 1 || index == 11 || index == 21 || index == 31 || index == 41 || index2 == 51)
            System.out.print("|");
         if (index % 10 != 0)
            System.out.print(element + " ");            
         else
            System.out.println(element + "|" );
      }
   }

}

我将50个整数随机生成的数组的输出复制4次到每个排序算法中:

Bubble Sort:
-----------------------------------------
|Original order:                        |
|572 125 205 590 213 611 983 631 111 711|
|195 305 185 432 914 555 655 432 475 156|
|873 447 276 856 767 126 626 149 383 711|
|766 917 628 342 527 976 751 429 280 392|
|526 568 199 877 796 515 292 454 444 617|
|                                       |
|Sorted order:                          |
|111 125 126 149 156 185 195 199 205 213|
|276 280 292 305 342 383 392 429 432 432|
|444 447 454 475 515 526 527 555 568 572|
|590 611 617 626 628 631 655 711 711 751|
|766 767 796 856 873 877 914 917 976 983|
|                                       |
|Swaps:557                              |
|Comparisons:1225                       |
-----------------------------------------
Insertion Sort: 
-----------------------------------------
|Original order:                        |
|572 125 205 590 213 611 983 631 111 711|
|195 305 185 432 914 555 655 432 475 156|
|873 447 276 856 767 126 626 149 383 711|
|766 917 628 342 527 976 751 429 280 392|
|526 568 199 877 796 515 292 454 444 617|
|                                       |
|Sorted order:                          |
|111 125 126 149 156 185 195 199 205 213|
|276 280 292 305 342 383 392 429 432 432|
|444 447 454 475 515 526 527 555 568 572|
|590 611 617 626 628 631 655 711 711 751|
|766 767 796 856 873 877 914 917 976 983|
|                                       |
|Swaps:557                              |
|Comparisons:606                        |
-----------------------------------------
Selection Sort: 
-----------------------------------------
|Original order:                        |
|572 125 205 590 213 611 983 631 111 711|
|195 305 185 432 914 555 655 432 475 156|
|873 447 276 856 767 126 626 149 383 711|
|766 917 628 342 527 976 751 429 280 392|
|526 568 199 877 796 515 292 454 444 617|
|                                       |
|Sorted order:                          |
|111 125 126 149 156 185 195 199 205 213|
|276 280 292 305 342 383 392 429 432 432|
|444 447 454 475 515 526 527 555 568 572|
|590 611 617 626 628 631 655 711 711 751|
|766 767 796 856 873 877 914 917 976 983|
|                                       |
|Swaps:49                               |
|Comparisons:240                        |
-----------------------------------------
Quick Sort: 
-----------------------------------------
|Original order:                        |
|572 125 205 590 213 611 983 631 111 711|
|195 305 185 432 914 555 655 432 475 156|
|873 447 276 856 767 126 626 149 383 711|
|766 917 628 342 527 976 751 429 280 392|
|526 568 199 877 796 515 292 454 444 617|
|                                       |
|Sorted order:                          |
|111 125 126 149 156 185 195 199 205 213|
|276 280 292 305 342 383 392 429 432 432|
|444 447 454 475 515 526 527 555 568 572|
|590 611 617 626 628 631 655 711 711 751|
|766 767 796 856 873 877 914 917 976 983|
|                                       |
|Swaps:155                              |
|Comparisons:252                        |
-----------------------------------------

在插入排序和选择排序时,我的朋友说交换和比较应该大致相同。我不确定我是否正在计算掉期和比较,而不是我应该如何做。这些也是我必须使用的算法;即使它们是最糟糕的情况算法。

2 个答案:

答案 0 :(得分:0)

互换和比较不应该彼此大致相同。您只能在比较告诉您之后进行交换。但是,如果你的朋友意味着这些排序算法之间没有太大差异,那么它就更主观了。泡泡排序是最慢的并不奇怪。

考虑重命名countcount2。这些都是可怕的名字。 countSwapscountComparisons要好得多。

while (scan > 0 && array[scan-1] > unsortedValue)

之后是

count2++

由于刚刚进行了两次比较,因此这是一个不足之处。

如果我这样做,我会采取懒惰的方式并编写两个辅助函数,每次调用它们都会计算。

public static int comparisionCount = 0;
public static int swapCount = 0;

public static void swap(int[] array, int i, int j) {
    int temp = array[j];
    array[j] = array[i];
    array[i] = temp;
    swapCount++;
}


public static boolean isGreater(int left, int right) {
    comparisionCount++;
    return left > right;
}

只使用那些进行掉期和比较,计算神奇地发生,而不必担心它。

例如:

while (scan > 0 && array[scan-1] > unsortedValue)

会变成:

`while(isGreater(scan,0)&amp;&amp;&amp; isGreater(array [scan-1],unsortedValue))

一点点挑选:

if (index == 1 || index == 11 || index == 21 || index == 31 || index == 41 || index2 == 51)

可能就是:

if (index % 10 == 1)

答案 1 :(得分:0)

前段时间我创建了一个可视化排序的测试程序。它附带了一个自定义数组类型,它记录了交换计数和比较计数 - 每次访问都是如此。

话虽如此,用n=1,000运行一些测试:

Bubble Sort:

n      | Comparisons | Swaps
-------+-------------+---------
1000   | 499455      | 252385
1000   | 496944      | 253857
1000   | 499122      | 251105


Insertion sort:

n      | Comparisons | Swaps
-------+-------------+---------
1000   | 249434      | 248438
1000   | 256309      | 255315
1000   | 246906      | 245910

通常,冒泡排序有更多的比较。这是因为必须将元素平均比较n/2次,重复n次。

这是因为第一个元素必须与下一个n-1元素进行比较,第二个元素必须与下一个n-2进行比较,依此类推。您可以在(1000 / 2) * 1000 = 500,000之后看到这一点,这是大多数运行中的几百个。


插入排序只需要到达一个小于临时元素的元素。因此,它会比较n/2次的平均值,直到找到较低的元素。它还必须执行n/2比较的平均值 - 因为工作阵列逐渐变大。因此(1000 / 2) * (1000 / 2) = 250000,在大多数运行中再次出现。{/ p>

第一个元素必须与一个值进行比较。第二个元素必须与一个值或两个值进行比较。第三个是一个,两个或三个值等等。

通过检查算法,您可以进行计算并进行证明。