I am getting a Segmentation fault when I run my code. It is happening right now when the 100,000th array location is being populated. But it has been at different spots when I ran it before. Any ideas??
/**
* @author Jason Mejak
*
* This program uses the implementation of the hybrid QuickSort/InsertionSort
* and uses a large array to test the speed of the sorts
*
* @date 5/17/07
*/
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
/**
* Sorts the items in an array into ascending order.
*
* @pre theArray is an array of n items.
*
* @post theArray is sorted into ascending order; n is unchanged.
*
* @param theArray The given array
*
* @param n The size of the array
*/
void InsertionSort(int theArray[], int n){
for(int unsorted = 1; unsorted < n; ++unsorted){
int nextItem = theArray[unsorted];
int loc = unsorted;
for(;(loc > 0) && (theArray[loc-1] > nextItem); --loc){
//Shift theArray[loc-1] to the right
theArray[loc] = theArray[loc - 1];
}
//insert nextItem into Sorted region
theArray[loc] = nextItem;
}
}
/**
* Partions an array for quicksort.
*
* @pre theArray[fisrt..last] is and array; first <= last.
*
* @post Partions theAray[first..last] such that:
* S1 = theArray[first..pivotIndex-1] < pivot
* theArray[pivotIndex] == pivot
* S2 = theArray[pivotIndex+1..last] >= pivot
*
* @param d The given array.
*
* @param left The first element to consider in theArray
*
* @param right The last elem to consider in theArray.
*/
int Partition( int d[], int left, int right){
int val =d;
int lm = left-1;
int rm = right+1;
for(;;) {
do
rm--;
while (d[rm] > val);
do
lm++;
while( d[lm] < val);
if(lm < rm) {
int tempr = d[rm];
d[rm] = d[lm];
d[lm] = tempr;
}
else
return rm;
}
}
void QuickSort (int A[], int F, int L){
int PivotIndex;
if (F < L){
//is this a small region?
if ((L - F) < 50){
//yes...so use InsertionSort
InsertionSort (A, L);
}else{
//no...do normal QuickSort
Partition (A, F, L);
QuickSort (A, F, PivotIndex-1);
QuickSort (A, PivotIndex+1, L);
}
}
}
int main(){
int randomArray[100000];
int cOff = 50;
for(int i = 0; i < 100000; i++){
randomArray[i] = rand();
cout<< i << "\n";
}
QuickSort(randomArray, 0, 200000);
}