Hello

Can someone tell me what this sort algorithm is called.?
And if possible a little bit of information about it...

void record_search(struct CdRecords cdDB[])
{
   system("CLS");
   int i;
   char name[20];
     
   printf("Enter Name:");
   scanf("%s", name);
   for(i = 0;i<datasize;i++)
   {
     if((strcmp(name,cdDB[i].Artist))==0)
     {
	 
         ENTER DATA TO BE DISPLAYED
	 }
   }
   printf("Press Enter To Continue");
  // fflush(stdin);
   getch();

}

Dani AI

Generated

As already pointed out, that routine is not a sort — it’s a linear (sequential) search: the code walks the array and uses strcmp to test each record until a match is found. As noted, this is called linear search. Complexity is O(n) time (best case O(1) if the first element matches) and O(1) extra space. Fine for small datasets or occasional lookups, but it becomes slow as the number of records grows.

If lookups will be frequent, consider one of three improvements: (1) sort the array once and use binary search (qsort + bsearch in C) — O(log n) per lookup; (2) build a hash-based index for average O(1) lookups; or (3) keep an auxiliary index (e.g., array of pointers) sorted by Artist so you can search without moving big records. Which option is best depends on how often records change versus how often you search.

A few practical, safety-oriented notes tied to the posts: don’t use scanf("%s", ...) without a width limit (risk of buffer overflow and it stops at whitespace — many artist names contain spaces). Prefer fgets and trim the newline:

char name[128];
if (fgets(name, sizeof name, stdin))
    name[strcspn(name, "\n")] = '\0';

For case-insensitive matches use strcasecmp (POSIX) or _stricmp (Windows), or normalize both strings to lower-case before comparing.

About memset: gave the prototype — memset fills raw bytes with the byte value given. Zeroing memory with memset is common, but strictly speaking setting pointer fields to all-bits-zero is not guaranteed by the C standard to produce a null pointer on every exotic platform; calloc or explicit initialization is safer and clearer. If you need to clear sensitive data, use platform-specific safe APIs (e.g., memset_s or explicit_bzero) so the compiler does not optimize the clear away. Also avoid system("CLS") and getch() if you want portable code.

Recommended Answers

All 5 Replies

I dont think it is a sorting function at all. All it does is ask the user for a name, searches the database for a match, and once a match is found display the information the user wants to see. Nope, no sort.

I dont think it is a sorting function at all. All it does is ask the user for a name, searches the database for a match, and once a match is found display the information the user wants to see. Nope, no sort.

My fault sorry,, i meant to ask what kind of SEARCH method is this... ?

My fault sorry,, i meant to ask what kind of SEARCH method is this... ?

Linear Search

Hey thanx for that one

also check this line out:

memset(cdDB,0,datasize*sizeof(struct CdRecords));

what does memset exactly do as i have neva encountered it before and want to know how it actually works.

Syntax

#include <string.h>
void *memset(void *s, int c, size_t n);

Description
memset() sets the first n bytes in memory area s to the value of c (converted to an
unsigned char). It returns s.

commented: Thanks +1
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.