TransWikia.com

Insertion Sort on a String Array

Stack Overflow Asked by 101001 on November 27, 2021

I am having trouble applying the insertion sort algorithm to the string because it. I have been getting various errors which I think are from issues regarding strings vs char types.

Ex:

candidate template ignored: could not match 'stack' against 'basic_string'
operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)

The insertion sort algorithm was pulled from geeks for geeks but I just changed it to string array.

void insertionSort(string arr[], int n)
{
    int i, key, j, unsortedness;
    for (i = 1; i < n; i++)
    {
        key = arr[i];
        j = i - 1;
        
        /* Move elements of arr[0..i-1], that are  
         greater than key, to one position ahead  
         of their current position */
        while (j >= 0 && arr[j] > key)
        {
            arr[j + 1] = arr[j];
            j = j - 1;
    
        }
        
        arr[j + 1] = key;
    }
}

int main()
{
    
    //Read in from file stuff missing to save space
    
    int d, lengthStrings, numberStrings; // D will hold the number of data sets
    infile >> d;
    cout << d << endl;
    while (d != 0)
    {
        infile >> lengthStrings;
        infile >> numberStrings;
        int numCopy = numberStrings;
        
        int i = 0;
        string arrayDna[numberStrings]; //char arrayDna[numberStrings][lengthStrings] instead?;
        
        while (numberStrings != 0)
        {
            infile >> arrayDna[i];
            i++;
            numberStrings--;
        }
        
        insertionSort(arrayDna[], numCopy);

        for (int i = 0; i < numCopy; i++)
            cout << arrayDna[i] << "n";
        
        d--;

So basically I need help fixing the error not allowing me to apply this insertion algorithm to my own string array.

One Answer

I didn't work on the logic, but cleared all the basic errors, hopefully:)

changes:

(1) arrayDna[] => arrayDna (in the parameters) while invoking insertionSort function.

(2) In the insertionSort function at line : key = arr[i],

key is an int type but needed string type, so changed type of key to string from int

void insertionSort(string arr[], int n)  
 {  
int i,j, unsortedness;  
string key;
for (i = 1; i < n; i++) 
{  
    key = arr[i];  
    j = i - 1;  

    /* Move elements of arr[0..i-1], that are  
    greater than key, to one position ahead  
    of their current position */
    while (j >= 0 && arr[j] > key) 
    {  
        arr[j + 1] = arr[j];  
        j = j - 1;
    // Since I just need to find unsortedness and not actually sort 
    //I should probably just replace the two lines with something such as  
    //unsortedness++ and compare that way
    }  

    arr[j + 1] = key;  
   }  
 }  



 int main(){

 //Read in from file stuff missing to save space

int d,lengthStrings, numberStrings;     // D will hold the number of data sets
infile >> d;
cout << d << endl;
while(d !=0){
    infile >> lengthStrings;
    infile >> numberStrings;
    int numCopy=numberStrings;

    int i=0;
    string arrayDna [numberStrings]; //char arrayDna[numberStrings][lengthStrings] instead?;
    


    while(numberStrings != 0){
        infile >> arrayDna[i];
        i++;
        numberStrings--;
    }
    
    insertionSort(arrayDna, numCopy);

 for (int i = 0; i < numCopy; i++) 
        cout << arrayDna[i] << "n";

    
    d--;
    }
  }

Answered by Sai Sreenivas on November 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP