Question

I have this function called WordSort(worddata W [], int count) that is fed two variables 1 - worddata is the array holding information on a given word in a file. count is just the counter variable to see which word in the array we are looking at.

the words.txt file that is read into this program would just be a string of words.

this is a list of words
there are letters and numbers
23 people recommend this program.

Heres the function:

void WordSort (worddata W [], int count)
{
  for (int i=1; i < count; i++)
         {
           for (int j=i; j > 0 && W[j-1].word > W[j].word; j--)
             {
               Swap(W[j], W[j-1]);
             }
         }
}

The swap function is suppose to swap every element with the one before it as long as j > 0 or the list is over. Im confused on how to complete the swap function, here's the example i was given.

void Swap (worddata & a, worddata & b)
{
 int += a;
 a = b;
 b =+;
}

Swap is suppose to swap every element with the one before it

I think the WordSort function works fine, the only thing missing is the Swap function. Could anyone point me in the right direction or explain insertion sorting better to me?

Was it helpful?

Solution 3

Swap should look like this -- I have no idea how your example is even close.

void Swap (worddata & a, worddata & b)
{
 worddata temp = a;
 a = b;
 b = temp;
}

OTHER TIPS

void insertion_sort()
{


    /* Algorithm : Insertion Sort
     * Coded by .
    */
    int num;
    /*
     * Asking the User no of Integers he/she wants to enter
     */
    cout << "Enter no of integers u want to enter: ";
    cin >> num;
    /* Creating an Array to store the integers*/
    int s[num];
    /*Taking Integers from the User */
    for(int i = 0 ; i < num ; i++)
    {
        cout << "Integer " << i+1 << " is : ";
        int x;
        cin >> x;
        s[i] = x;
    }
    /* The Magic of INSERTION SORT */
    for(int j = 1 ; j <= (num-1) ; j++)
    {
        int key = s[j]; 
        int k = j-1;

        while(k >=0 && key <= s[k])
        {
            s[k+1] = s[k];
            k = k - 1;
        }
        s[k+1]=key;

    }
    /*Printing Out the Sorted List */
    cout << "The Sorted List is \n\n";
    for(int i = 0 ; i < num ; i++)
    {
        cout << s[i] << "  ";
    }

}

Use standard library std::swap instead. In your loop:

for (...)
{
    std:swap(W[j], W[j-1]);
}

std::swap requires worddata class to have a copy constructor and an assignment operator defined explicitly or implicitly.

Insertion sort using "for loop" (2 iterations)

#include<iostream>
using namespace std;


int insertion(int arr[], int size_arr)
{
    int i,j,n, temp;

    for(i=1;i<size_arr; i++){
            j=i-1;
            temp = arr[i];
            for (j; j >=  0; j--)
        {
            if(arr[j] > temp){
                        arr[j+1] = arr[j];
                        arr[j] = temp;
            }
        }
            arr[j] = temp;
      }

    for(i=0;i<size_arr;i++){
        cout<<arr[i]<<endl;
    }
    return 0;
}

int main(){
    int arr[] = {3,38,1,44,66,23,105,90,4,6};
    int size_arr = sizeof(arr) / sizeof(arr[0]);
    insertion(arr,size_arr);
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top