Looking for help understanding sorts

Discussion in 'OT Technology' started by butaro, Sep 10, 2005.

  1. butaro

    butaro New Member

    Joined:
    Mar 2, 2005
    Messages:
    1,129
    Likes Received:
    0
    Location:
    Ontario
    This is for a school assignment.
    My first problem is a syntax problem which i couldnt solve for about 1 hr search my books and the internet.

    I am doing a selection sort with C style strings and when i do the index swap from the array i get a pain in the ass error that im not sure how to fix, here is my selection sort function code.
    Code:
    void SelectionSort(char** papszWords, int nSize)
      {	char* pszLowest;
      	for (int i=0;i<(nSize-1);i++)
      	{   int iLowestIndex = i;
      		for (int j=i+1;j<nSize;j++)
     		{	if (stricmp(papszWords[j],papszWords[iLowestIndex])<0)
      				iLowestIndex = j;
      		}
      //The Following part is incomplete...
      		if (iLowestIndex>i)
      		{
      			//pszLowest=new char[strlen(papszWords[i])]; 
      			pszLowest=papszWords[i];
      			delete[]papszWords[i];
     			//papszWords[i]=new char[strlen(papszWords[iLowestIndex])];
      			papszWords[i]=papszWords[iLowestIndex];
      			delete[]papszWords[iLowestIndex];
      			//papszWords[iLowestIndex]=new char[strlen(pszLowest)];
      			delete[]pszLowest;
      		}
      	}
      }  // end SelectionSort()
    What im pretty much trying to do in the last part is...
    temp string=current string
    current string=OldLowest string
    OldLowest string=temp string
    It's a dynamic memory problem and i always seem to have problems when it comes to pointers.

    I'll post the second problem after i hit the 1 hour mark being stuck.
     
  2. butaro

    butaro New Member

    Joined:
    Mar 2, 2005
    Messages:
    1,129
    Likes Received:
    0
    Location:
    Ontario
    LOL 5 minutes later i figure out my problem, shouldnt be deleting pointers/creating new ones because they are the same size.

    Well now, im looking to create a partition(quicksort) function and im really confused how to start it.
     

Share This Page