Обучение сортировке нужна помощь? - PullRequest
0 голосов
/ 10 июля 2011

У меня есть код ниже, который я нашел.Я пытаюсь узнать, какой метод сортировки является самым быстрым, а какой использует больше всего и меньше всего сравнений.У кого-нибудь есть идеи, как я могу добавить сюда код для этого?Я хочу подсчитать общее количество сравнений каждого вида.

//***********************************************************************************
//  Sorting.java
//
//  Contains various sort algorithms that operate on an array of comparable objects.
//
//************************************************************************************

public class Sorting

{

//------------------------------------------------------------------------------------
//  Sorts the specified array of integers using the selection sort algorithm.
//------------------------------------------------------------------------------------

public static void selectionSort (Comparable[] data)
{
  int min;

  for (int index = 0; index < data.length-1; index ++)
  {
     min = index;
      for (int scan = index+1; scan < data.length; scan++)
        if (data[scan].compareTo(data[min]) < 0)
            min = scan;

      swap (data, min, index);

  }
}
//---------------------------------------------------------------------------------------
//  Swaps to elements in the specified array.
//---------------------------------------------------------------------------------------

private static void swap (Comparable[] data, int index1, int index2)
{
   Comparable temp = data[index1];
    data[index1] = data[index2];
    data[index2] = temp;

}

//---------------------------------------------------------------------------------------
//  Sorts the specified array of objects using an insertion sort algorithm.
//---------------------------------------------------------------------------------------

public static void insertionSort (Comparable[] data)
{
  for (int index = 1; index < data.length; index++)
  {
    Comparable key = data[index];
     int position = index;

     // shift larger values to the right
     while (position > 0 && data[position-1].compareTo(key) > 0)
     {
       data[position] = data[position-1];
        position--;
     }

     data[position] = key;

    }
}

//---------------------------------------------------------------------------------------
//  Sorts the specified array of objects using a bubble sort algorithm.
//---------------------------------------------------------------------------------------

public static void bubbleSort (Comparable[] data)
{
  int position, scan;

  for (position = data.length - 1; position >= 0; position--)
  {
     for (scan = 0; scan <= position - 1; scan ++)
       if (data[scan].compareTo(data[scan+1]) >0)
          swap (data, scan, scan+1);

    }
}

//---------------------------------------------------------------------------------------
//  Sorts the specified array of objects using the quick sort algorithm.
//---------------------------------------------------------------------------------------

public static void quickSort (Comparable[] data, int min, int max)
{
  int pivot;

  if (min < max)
  {
    pivot = partition (data, min, max); // make partitions
     quickSort(data, min, pivot-1);  //sort left paritions
     quickSort(data, pivot+1, max);  //sort right paritions
  }
}
//---------------------------------------------------------------------------------------
//  Creates the partitions needed for quick sort.
//---------------------------------------------------------------------------------------

public static int partition (Comparable[] data, int min, int max)
{
  //Use first element as the partition value
  Comparable partitionValue = data[min];

  int left = min;
  int right = max;

  while (left < right)
  {
    // Search for an element that is greater than the partition element
     while (data[left].compareTo(partitionValue) <= 0 && left < right)
       left++;

    // Search for an element that is less than the partition element
    while (data[right].compareTo(partitionValue) > 0)
      right--;

    if (left < right)
      swap (data, left, right);

    }

    // Move the partition element to its final position
    swap (data, min, right);

    return right; 
  }

//---------------------------------------------------------------------------------------
//  Sorts the specified array of objects using the merge sort algorithm.
//---------------------------------------------------------------------------------------

public static void mergeSort (Comparable[] data, int min, int max)
{
  if (min < max)
  {
    int mid = (min + max) / 2;
     mergeSort(data, min, mid);
     mergeSort(data, mid+1, max);
     merge (data, min, mid, max);

  }
 }

//---------------------------------------------------------------------------------------
//  Sorts the specified array of objects using the merge sort algorithm.
//---------------------------------------------------------------------------------------

public static void merge (Comparable[] data, int first, int mid, int last)
{
  Comparable[] temp = new Comparable[data.length];

  int first1 = first, last1 = mid; //endpoints of first subarray
  int first2 = mid + 1, last2 = last; //endpoints of second subarray
  int index = first1; // next index open in temp array

  // Copy smaller item from each subarry into temp until one of the subarrays is exhausted

  while (first1 <= last1 && first2 <= last2)
  {
    if (data[first1].compareTo(data[first2]) < 0)
     {
       temp[index] = data[first1];
        first1++;
     }
     else
     {
       temp[index] = data[first2];
        first2++;
     }
    index++;
  }
 //  Copy remaining elements from first subarray, if any
 while (first1 <= last1)
 {
   temp[index] = data[first1];
    first1++;
    index++;
 }

 //  Copy remaining elements from second subarray, if any
 while (first2 <= last2)
 {
   temp[index] = data[first2];
    first2++;
    index++;
 }

 // Copy merged data into original array
 for (index = first; index <= last; index++)
   data[index] = temp[index];
 }
 }

Ответы [ 3 ]

5 голосов
/ 10 июля 2011

Если вас интересует скорость, используйте Arrays.sort () .Если вы академически интересуетесь тем, что связано с различными техниками сортировки, возможно, вам будет проще просто взглянуть на Википедию .Если вы хотите, чтобы мы сделали вашу домашнюю работу для вас ... мы не будем, извините.

Редактировать: Я думаю, это справедливо для меня, чтобы сказать это: Есть ли причина, по которой вы не могли просто инициализировать целое числодо 0 в начале каждого метода, увеличивать его каждый раз, когда происходит что-то интересное, а затем печатать в конце?

1 голос
/ 10 июля 2011

Вы должны определить абстрактный класс, который реализует подсчет, а затем некоторые производные классы, которые реализуют алгоритмы. Основная программа будет выглядеть так:

List<String> list = new ArrayList<String>();
// TODO: add some elements to the list
SortingAlgorithm alg = new BubbleSort();
alg.sort(list);
alg.printSummary();

Теперь абстрактный класс, который реализует подсчет:

public abstract class SortingAlgorithm<T> {

  /* the actual algorithm, which uses the compare and swap methods. */
  public abstract void sort(List<T> list);

  private long compares = 0;
  private long swaps = 0;

  protected int compare(T a, T b) {
    compares++;
    return a.compareTo(b);
  }

  protected void swap(int index1, int index2) {
    swaps++;
    // TODO: do the actual swapping
  }

  public void printSummary() {
    // TODO
  }
}

Конкретные алгоритмы теперь должны реализовывать только метод sort. Как это сделать, оставлено в качестве упражнения для читателя.

1 голос
/ 10 июля 2011

Скорее узнайте об O-нотации из хорошей книги, например:

http://www.amazon.com/Data-Structures-Algorithms-Java-2nd/dp/0672324539

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...