Example #1
0
int Sorts<T>::partition(T arr[], int first, int last, bool median){
	int pivotIndex = last;
	int leftIndex=first;
	int rightIndex = (pivotIndex-1);
	if(median){
		setMedianPivot(arr, first, last);
	}	
	bool notDone = true;
	while(notDone){
			while((arr[leftIndex]<arr[pivotIndex])&&(leftIndex<(last+1))){
				leftIndex++;
			}
			while((arr[rightIndex]>= arr[pivotIndex])&&(rightIndex>0)){
				rightIndex--;
			}
			if(leftIndex<rightIndex){
				swap(arr, leftIndex, rightIndex);
			}
			else{
				notDone = false;
				swap(arr, leftIndex, pivotIndex);
				
			}
		
	}

	return leftIndex; //now holds the pivot value, in the correct position
	
}
int QuickSort<T>::partition(T arr[], int first, int last, bool median) {
	if(median == true) {
		setMedianPivot(arr, first, last);
	}

	T pivot = arr[last];
	int partitionIndex = first;

	for(int i = first; i<last; ++i) {
		if(arr[i] <= pivot) {
			T swap = arr[i];
			arr[i] = arr[partitionIndex];
			arr[partitionIndex] = swap;
			++partitionIndex;
		}
	}
	T swap = arr[partitionIndex];
	arr[partitionIndex] =  arr[last];
	arr[last] = swap;

	return partitionIndex;
}
Example #3
0
int Sorts<T>::partition(T arr[], int first, int last, bool median)
{
    if (median == true)
    {
        setMedianPivot(arr, first, last);
    }
    T pivot = arr[last];
    int storeIndex = first;
    for (int i = first; i < last; i++)
    {
        if (arr[i] <= pivot)
        {
            T temp = arr[i];
            arr[i] = arr[storeIndex];
            arr[storeIndex] = temp;
            storeIndex ++;
        }
    }
    T temp = arr[last];
    arr[last] = arr[storeIndex];
    arr[storeIndex] = temp;
    return storeIndex;
}
Example #4
0
int Sorts<T>::partition(T arr[], int first, int last, bool median)
{
    if (median)
    {
        if ((last+1)>3)
        {
            setMedianPivot(arr, first, last);
        }

    }
        int pivot = arr[last];

        while (first<last)
        {
            while (arr[first]<pivot)
                first++;
            while (arr[last]>pivot)
                last--;
            if(arr[first]== arr[last])
            {
                first++;
            }
            else if (first<last)
            {
                int temp = arr[first];

                arr[first] = arr[last];

                arr[last] = temp;
            }

        }


    return last;
}
Example #5
0
int Sorts<T>::partition(T arr[], int first, int last, bool median)
{
    if(median == false)
    {
        int p = last;
        int l = first;
        int r = last - 1;

        while(l < r)
        {

            while(arr[l] < arr[p])
            {
                if( l == r)
                    break;
                else
                {
                    l++;
                }


            }
            while(arr[r] > arr[p])
            {
                if( r == l)
                    break;
                else
                {
                    r--;
                }
            }

            if(arr[l] == arr[r])
            {
                l++;
            }
            else if(l < r)
            {
                int temp = arr[l];
                arr[l] = arr[r];
                arr[r] = temp;

            }
        }

        if(arr[r] > arr[p])
        {
            int temp = arr[p];
            arr[p] = arr[r];
            arr[r] = temp;
        }

        return r;
    }
    else //median is true
    {
        setMedianPivot(arr, first, last); //arr[m] becomes arr[last]
        int p = last;
        int l = first;
        int r = last - 1;

        while(l < r)
        {
            while(arr[l] < arr[p])
            {
                if( l == r )
                    break;
                else
                    l++;
            }
            while(arr[r] > arr[p])
            {
                if( r == l )
                    break;
                else
                    r--;
            }

            if(arr[l] == arr[r])
            {
                if( l == r )
                    break;
                l++;
            }
            else if(l < r)
            {
                int temp = arr[l];
                arr[l] = arr[r];
                arr[r] = temp;
                if(l + 1 == r)
                    break;
            }
        }

        if(arr[r] > arr[p])
        {
            int temp = arr[p];
            arr[p] = arr[r];
            arr[r] = temp;
        }


        return r;
    }
}