1

I tried writing my own insertion sort and swap functions in C. The insertion sort or the swap is compiling but not working yet.

input: gcc insertion.c -o insertion ./insertion alfalfa

Input: alfalfa

Output: alfalfa

#include <stdio.h>
#include <string.h>
#define SORTL 20

char * insertionsort(int *countargs, char *word);
void swap(char *a, char *b);

/* Enter word to insertion sort at the terminal */
int main(int argc, char *argv[]){

    /* Pass arguments from command line */
    if(argc != 2)
        perror("Please enter two arguments.");
    int argcount = strlen(argv[1]);
    char presort[SORTL];
    strcpy(presort, argv[1]);
    char * resultword;

    resultword = insertionsort( &argcount, presort );

    printf("Presort: %s\nPostsort: %s", presort, resultword);

    return 0;
}

char * insertionsort(int *countargs, char word[]){

    int i, j;

    for( i = 1; i < *countargs; i++) {
        j = i;
        while( (j < 0) && (word[j] < word[j-1]) ) {
            swap( &word[j], &word[j-1] );
            j = j - 1;
        }
    }
    return word;
    }

void swap(char *a, char * b)
{
   char temp;

   temp = b;
   b   = a;
   a   = temp;   
}
Mohan
  • 1,871
  • 21
  • 34
Julian Wise
  • 380
  • 1
  • 3
  • 16

2 Answers2

3

You need to change the swap function

void swap(char *a, char * b)
{
    char temp;

    temp = *b;
    *b   = *a;
    *a   = temp;   
}

Since, in swap() you need to swap the characters in the memory, and not the address to which the variables are pointing at.


Another thing, by the printf() statement in the end i feel you want to print the older unsorted and the newer sorted string. If so, then it will not work. Only one string will be printed, since essentially you are swapping the characters in the initial string only and resultword is pointing at the same string,

resultword = insertionsort( &argcount, presort );
//sending presort and receiving it in word in the function insertionsort
//receiving the return in resultword

&

return word;
//and returning back the same address from insertionsort

EDIT

The condition in your while loop is incorrect. It should be j > 0

while( (j > 0) && (word[j] < word[j-1]) )

Since you are starting from the far end and going to the beginning.

Haris
  • 12,120
  • 6
  • 43
  • 70
1

1. In this function -

void swap(char *a, char * b)
{ 
   char temp;            
   temp = b;             //assigining char * to char 
   b   = a;
   a   = temp;           // same here 
}

a and b are char * , dereference pointer a and b and then swap -

void swap(char *a, char * b)
{
   char temp;
   temp = *b;
   *b   = *a;
   *a   = temp;   
}

2. In your function -

for( i = 1; i < *countargs; i++) {
    j = i;
    while( (j < 0) && (word[j] < word[j-1])){   //this loop will not works as j is not < 0
        swap( &word[j], &word[j-1] );
        j = j - 1;
    }
}

Your while loop will not iterate as from starting j is not less than 0 , so second condition is not checked . Inside while loop this condition j<0 should be j>0 .

ameyCU
  • 16,489
  • 2
  • 26
  • 41