Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6111899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:41:01+00:00 2026-05-23T14:41:01+00:00

I have the code below that I have found. I am trying to learn

  • 0

I have the code below that I have found. I am trying to learn which sorting method is the fastest and which ones use the most and least comparisons. Anyone have any idea how I can add some code in here to do that? I want to tally the total number of comparisons of each sort.

//***********************************************************************************
//  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];
 }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:41:01+00:00Added an answer on May 23, 2026 at 2:41 pm

    If you’re interested in speed, use Arrays.sort(). If you’re academically interested in what’s involved in various sorting techniques, it’s probably faster to just look at Wikipedia. If you want us to do your homework for you…we won’t, sorry.

    Edit: I guess it’s fair for me to say this: Is there any reason you couldn’t just initialize an integer to 0 at the start of each method, increment it every time something interesting happened, and then print it at the end?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to use a plist to store an array with the below
I am trying to code a little bit of math with java. What I
I have a bunch of strings in my resource(.resx) file. I am trying to
I am trying to achieve a JQuery AJAX call to a controller action method
I am new to AES encryption but trying to build a solution which: Accepts
I'm using C and trying to get access to the constant M_PI (3.14159...). I
I have a model tied to an array controller. I need to be able
I'm working on an Android application that will send MMS internally without using the
I am getting this error message: --------------------------- You have an error in your SQL
In the getter for one of my retained properties, I allocate and assign an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.