简体   繁体   中英

How do I add comparisons and switches to quicksort and merge sort?

I created code for quicksort and mergesort and now I'm trying to add a comparison count and switch count to the code but I can't figure out where to add "comparisons++" and "switches++".

My code for quick sort

public void quickSort( int start, int finish )
{
    comparisons = 0;
    switches = 0;
    int[] temp = new int[numbers.length];
    int lowSpot = start;
    int hiSpot = finish;
    int pivot = (int)(Math.random()*(finish+1-start)) + start;
    for( int i = start; i <= finish; i++ )
    {
        if( i != pivot )
        {
            if( numbers[i] <= numbers[pivot] )
            {
                temp[lowSpot] = numbers[i];
                lowSpot++;
            }
            else
            {
                temp[hiSpot] = numbers[i];
                hiSpot--;
            }
        }
    }
    temp[lowSpot] = numbers[pivot];
    for( int j = start; j <= finish; j++ )
    {
        numbers[j] = temp[j];
    }

    if( start < lowSpot - 1 )
    {
        quickSort( start, lowSpot - 1 );
    }
    if( hiSpot + 1 < finish )
    {
        quickSort( hiSpot+1, finish );
    }
    }

My code for merge and mergesort

public int[] merge( int[] one, int[] two )
{
    comparisons = 0;
    switches = 0;
    one = mergeSort( one );
    two = mergeSort( two );
    int[] merged = new int[one.length+two.length];
    int oneLoc = 0;
    int twoLoc = 0;
    for( int i = 0; i < merged.length; i++ )
    {
        if( oneLoc < one.length && twoLoc < two.length )
        {
            if( one[oneLoc] <= two[twoLoc] )
            {
                merged[i] = one[oneLoc];
                oneLoc++;
            }
            else
            {
                merged[i] = two[twoLoc];
                twoLoc++;
            }
        }
        else if( oneLoc < one.length )
        {
            merged[i] = one[oneLoc];
            oneLoc++;
        }
        else
        {
            merged[i] = two[twoLoc];
            twoLoc++;
        }
    }
    return merged;
}

public int[] mergeSort( int[] temp )
{
    if( temp.length == 1 )
    {
        return temp;
    }
    else if( temp.length == 2 )
    {
        if( temp[0] > temp[1] )
        {
            int holder = temp[0];
            temp[0] = temp[1];
            temp[1] = holder;
        }
        return temp;
    }
    else
    {
        int mid = temp.length / 2;
        int[] first = new int[mid];
        int[] second = new int[temp.length-mid];
        for( int i = 0; i < temp.length; i++ )
        {
            if( i < mid )
            {
                first[i] = temp[i];
            }
            else
            {
                second[i-mid] = temp[i];
            }
        }
        return merge( first, second );
    }
    }

A comparison is when you are....comparing variables (ie start < lowSpot - 1). Switch would be in the else blocks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM