Hi, I have a problem here in the selection search because it reads the text but does not detect the position of the text like a is in the 2 position. the is in the 1 positon.... like that. But If I do this for characters it detects it. Any suggestions? here is my code:

#include <stdio.h>
#include <conio.h>
#include <string.h>

void selection(char a[], int l, int r)
  { int i, j, t;
    for (i = l; i < r; i++)
      { int min = i;
        for (j = i+1; j <= r; j++) 
            if (a[j] <  a[min]) min = j;

      } 
  };
main(void)
{
  int i;
  char *list[ 5 ] = {"the", "a", "1", "some", "any" };  
  char findme[5];
  int found;
  selection(*list,0,5);     
  printf("Enter a character to find: ");
  scanf("%s", &findme);
  if (found == -1) printf("\n\n%s is not in the list.",findme);
  else printf("\n\n%s is in position %d of the list.",findme,found);
  getch();
};

Recommended Answers

All 3 Replies

You are confusing characters positions with string positions. Your program is not sorting the string in the list array but attempting to sort the characters in just one of the strings. And it doesn't do that very well either.

1) go back and review the selection sort algorithm and compare it with what you have posted.

2) review your program requirements that you were assigned because I think you probably misunderstood it. Post it here if you want to.

Here is my updated code... Can you check where I am wrong.

#include <stdio.h>
#include <conio.h>
#include <string>

void selection(char a[], int l, int r)
  { int i, j, t;
    for (i = l; i < r; i++)
      { int min = i;
        for (j = i+1; j <= r; j++) 
            if (a[j] <  a[min]) min = j;

      } 
  };
int binarysearch(char a[], char v[], int l, int r)
  { 
    while (r >= l)
      { int m = (l+r)/2;
        int x;
        if (v[x] == a[m]) return m;
        if (v[x] < a[m]) r = m-1; else l = m+1;
      }
    return -1;
  };
main(void)
{
  int i;
  char *list[ 5 ] = {"the", "a", "1", "some", "any"};  
  char findme[5];
  int found;
  selection(*list,0,5);     
  printf("Enter a character to find: ");
  scanf("%s", &findme);
  found = binarysearch(*list,findme,0,5);
  if (found == -1)
  printf("\n\n%s is not in the list.",findme);
  else printf("\n\n%s is in position %d of the list.",findme,found);
  getch();
};

Here is the selection sort algorithm. Compare it with yours. You are missing the swapping part at the end of the code snippet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.