Pls Help me, i am nearing the deadline July 30 of this case study and the code is almost finshed it does sort the input elements the way my algo logic does however i am not able to display the correct number of passes to show the law of the algo, it shoud be:

number of passes = total number of elements / 2
if total number of elements / 2 = .5 round off to left subgroup

the main logic of the algo is to divide it into two groups and sort it in either ascending or descending order, for example:

total number of elements = 6
order: descending

element 1: 5
element 2: 4
element 3: 6
element 4: 8
element 5: 2
element 6: 1

Original: 5 4 6 | 8 2 1
| = MID

Pass 1: the underlined elements are the numbers involved to be sorted, they shoud be swapped if ever the right first element of the right subgroup (in this case 1) is greater than the left subgroup (in this case 5)
5 4 6 | 8 2 1

Pass 3: then increase the number of elements to be sorted on both sides and repeat the seapping process as long as it satisfies the condition.
8 6 5 | 4 2 1

The problem is it didn't print out Pass 2: which should be 5 4 6 | 8 2 1

Here is the code:

#include"stdio.h"
#include"stdlib.h"

void read_list(int element[], int total,char choice)
{
  int ctr;

	if (choice == 'a' || choice == 'A')
	{
		for(ctr=0;ctr<total;ctr++)
		{
			printf("\n Enter Element [%d] ::" ,ctr);
			scanf("%s", &element[ctr]);
		}
	}
	if (choice == 'b' || choice == 'B')
	{
		for(ctr=0;ctr<total;ctr++)
		{
			printf("\n Enter Element [%d] ::" ,ctr);
			scanf("%d", &element[ctr]);
		}
	}
}


void print_list(int element[], int total,char choice)
{
  int ctr;

	if (choice == 'a' || choice == 'A')
	{
		for(ctr=0;ctr<total;ctr++)
			printf("  %c", element[ctr]);
	}
	if (choice == 'b' || choice == 'B')
	{
		for(ctr=0;ctr<total;ctr++)
			printf("  %d", element[ctr]);
	}
}


void insert_sort(int element[], int total,char choice)
{
	int pt, mid, left, right, ctr, ctr2, ctr3, temp;

  mid = total/2;
	pt = 0;
	ctr3 = 0;
	while (pt++ < mid)
	{

		for(ctr=total-1, ctr2=0; ctr>=mid && ctr2 <mid; ctr--, ctr2++)
		{
			if (element[ctr2] < element[ctr])
			{
				temp = element[ctr];
				element[ctr] = element[ctr2];
				element[ctr2] = temp;

				printf("\n Go Pass %d :: ", ctr2);
				print_list(element,total,choice);
			}

			if ((element[ctr2] >= element[ctr]) && ctr3 == 0)
			{
				ctr3 = 1;
				printf("\n To Pass %d :: ", ctr2);
				print_list(element,total,choice);
			}
			for (ctr3=0; ctr3<mid; ctr3++)
			{
				for (left=0; left<mid; left++)
				{
					if (element < element [left+1])
					{
						temp = element[left+1];
						element[left+1] = element;
						element = temp;
					}
				}

				for (right=total-1; right>=mid; right--)
				{
					if (element[ctr-1] < element[ctr])
					{
						temp = element[ctr];
						element[ctr] = element[ctr-1];
						element[ctr-1] = temp;
					}
				}
			}
		}
	}
	pt++;
	printf("\n Pass %d :: ", ctr2);
	print_list(element,total,choice);

}

void main()
{
  int element[20], total;
	char choice;
	clrscr();

	printf("Choose: A - Alphanumeric B - Numeric :: ");
	scanf("%c", &choice);

  printf("\n Enter Array Length :: ");
  scanf("%d", &total);

	read_list(element,total,choice);

	printf("\n The Array Elements are as follows :: ");
	print_list(element,total,choice);

	insert_sort(element,total,choice);

	printf("\n\n Final Answer :: ");
	print_list(element,total,choice);

	getch();
}

Pls Help me T___T

We all know that creating your own algorithm should have a law, but is it possible that the number of passes it makes may be lesser or more than the average performance?

example:

average case: number of passes = total elements / 2

best case: number of passes < (total element / 2)

worst case: number of passes > (total element /2) but not more than total element

Pls help me, i would like to hear your comments about this one.

i really need any help i can get, hmm.. and i would like to thank adak for helping me with this last time, however i wasn't able to clearly understand what you taught of me in my last thread

Hee is the re-written code @ Ancient Dragon,

#include"stdio.h"
#include"stdlib.h"

void read_list(int element[], int total,char choice)
{
  int ctr;

	if (choice == 'a' || choice == 'A')
	{
		for(ctr=0;ctr<total;ctr++)
		{
			printf("\n Enter Element [%d] ::" ,ctr);
			scanf("%s", &element[ctr]);
		}
	}
	if (choice == 'b' || choice == 'B')
	{
		for(ctr=0;ctr<total;ctr++)
		{
			printf("\n Enter Element [%d] ::" ,ctr);
			scanf("%d", &element[ctr]);
		}
	}
}


void print_list(int element[], int total,char choice)
{
  int ctr;

	if (choice == 'a' || choice == 'A')
	{
		for(ctr=0;ctr<total;ctr++)
			printf("  %c", element[ctr]);
	}
	if (choice == 'b' || choice == 'B')
	{
		for(ctr=0;ctr<total;ctr++)
			printf("  %d", element[ctr]);
	}
}


void insert_sort(int element[], int total,char choice)
{
	int pt, mid, left, right, ctr, ctr2, ctr3, temp;

  mid = total/2;
	pt = 0;

	while (pt++ < mid)
	{
		for(ctr=total-1, ctr2=0; ctr>=mid && ctr2 <mid; ctr--, ctr2++)
		{
			if (element[ctr2] > element[ctr])
			{
				temp = element[ctr];
				element[ctr] = element[ctr2];
				element[ctr2] = temp;

				printf("\n Pass %d :: ", ctr2);
				print_list(element,total,choice);

			}

			/*
			printf("\n Pass %d :: ", ctr2);
			print_list(element,total,choice);
			*/

			for (ctr3=0; ctr3<mid; ctr3++)
			{
				for (left=0; left<mid; left++)
				{
					if (element > element [left+1])
					{
						temp = element[left+1];
						element[left+1] = element;
						element = temp;
					}
				}

				for (right=total-1; right>=mid; right--)
				{
					if (element[ctr-1] > element[ctr])
					{
						temp = element[ctr];
						element[ctr] = element[ctr-1];
						element[ctr-1] = temp;
					}
				}
			}
		}
	}
	pt++;

	printf("\n Pass %d :: ", ctr2);
	print_list(element,total,choice);

}

void main()
{
  int element[20], total;
	char choice;
	clrscr();

	printf("Choose: A - Alphanumeric B - Numeric :: ");
	scanf("%c", &choice);

  printf("\n Enter Array Length :: ");
  scanf("%d", &total);

	read_list(element,total,choice);

	printf("\n The Array Elements are as follows :: ");
	print_list(element,total,choice);

	insert_sort(element,total,choice);

	printf("\n\n Final Answer :: ");
	print_list(element,total,choice);

	getch();
}

GCC doesn't like your code.

sort.c: In function ‘read_list’:
sort.c:13: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int *’
sort.c: In function ‘insert_sort’:
sort.c:75: warning: comparison between pointer and integer
sort.c:78: warning: assignment makes integer from pointer without a cast
sort.c:79: warning: assignment makes pointer from integer without a cast
sort.c: At top level:
sort.c:102: warning: return type of ‘main’ is not ‘int’
sort.c: In function ‘main’:
sort.c:106: warning: implicit declaration of function ‘clrscr’
sort.c:124: warning: implicit declaration of function ‘getch’
/tmp/ccNDVhKN.o: In function `main':
sort.c:(.text+0x3d1): undefined reference to `clrscr'
sort.c:(.text+0x489): undefined reference to `getch'
collect2: ld returned 1 exit status

GCC doesn't have clrscr() or getch() -- they're non-standard functions, after all -- but if your compiler does you can ignore those errors.

main() should really return int instead of void, though.

And the other warnings I think are important.

printf("\n Enter Element [%d] ::" ,ctr);
scanf("%s", &element[ctr]);

Should be %d, no?

[edit] Ah, I see that you really do want to store a string. Well, you can't do that when you have an array of ints. I suggest either using a struct/union with int and char[] members, or two separate arrays. [/edit]

if (element > element [left+1])
/* ... */
element[left+1] = element;
element = temp;

I think perhaps you wanted this:

if (element > element [left+1])
/* ... */
element[left+1] = element;
element = temp;

[edit] With those modifications, numeric sorting works as far as I can tell. Though the code could be a lot better -- with something like this, for example:

void order(int *a, int *b) {
    if(*a > *b) {
        int temp = *a;
        *a = *b;
        *b = temp;
    }
}

Alphanumeric sorting doesn't work, of course, because you can't read a string into a number. [/edit]

[edit=2] In case you're interested, here's my modified code. Sorry about the length, everyone.

#include"stdio.h"
#include"stdlib.h"

void read_list(int element[], int total,char choice)
{
  int ctr;

        if (choice == 'a' || choice == 'A')
        {
                for(ctr=0;ctr<total;ctr++)
                {
                        printf("\n Enter Element [%d] ::" ,ctr);
                        scanf("%d", &element[ctr]);
                }
        }
        if (choice == 'b' || choice == 'B')
        {
                for(ctr=0;ctr<total;ctr++)
                {
                        printf("\n Enter Element [%d] ::" ,ctr);
                        scanf("%d", &element[ctr]);
                }
        }
}


void print_list(int element[], int total,char choice)
{
  int ctr;

        if (choice == 'a' || choice == 'A')
        {
                for(ctr=0;ctr<total;ctr++)
                        printf("  %c", element[ctr]);
        }
        if (choice == 'b' || choice == 'B')
        {
                for(ctr=0;ctr<total;ctr++)
                        printf("  %d", element[ctr]);
        }
}


void insert_sort(int element[], int total,char choice)
{
        int pt, mid, left, right, ctr, ctr2, ctr3, temp;

  mid = total/2;
        pt = 0;

        while (pt++ < mid)
        {
                for(ctr=total-1, ctr2=0; ctr>=mid && ctr2 <mid; ctr--, ctr2++)
                {
                        if (element[ctr2] > element[ctr])
                        {
                                temp = element[ctr];
                                element[ctr] = element[ctr2];
                                element[ctr2] = temp;

                                printf("\n Pass %d :: ", ctr2);
                                print_list(element,total,choice);

                        }

                        /*
                        printf("\n Pass %d :: ", ctr2);
                        print_list(element,total,choice);
                        */

                        for (ctr3=0; ctr3<mid; ctr3++)
                        {
                                for (left=0; left<mid; left++)
                                {
                                        if (element > element [left+1])
                                        {
                                                temp = element[left+1];
                                                element[left+1] = element;
                                                element = temp;
                                        }
                                }

                                for (right=total-1; right>=mid; right--)
                                {
                                        if (element[ctr-1] > element[ctr])
                                        {
                                                temp = element[ctr];
                                                element[ctr] = element[ctr-1];
                                                element[ctr-1] = temp;
                                        }
                                }
                        }
                }
        }
        pt++;

        printf("\n Pass %d :: ", ctr2);
        print_list(element,total,choice);

}

int main()
{
  int element[20], total;
        char choice;

        printf("Choose: A - Alphanumeric B - Numeric :: ");
        scanf("%c", &choice);

  printf("\n Enter Array Length :: ");
  scanf("%d", &total);

        read_list(element,total,choice);

        printf("\n The Array Elements are as follows :: ");
        print_list(element,total,choice);

        insert_sort(element,total,choice);

        printf("\n\n Final Answer :: ");
        print_list(element,total,choice);
        
        return 0;
}

[/edit]

Comments
good help

That last post was getting crowded so I thought I'd start another one -- especially since it's a higher-level one.

Since you're trying to handle both numbers and strings, why not have two separate functions: one to read an array of numbers, and the other an array of strings? The functions themselves could declare the necessary arrays, and then call the sorting function.

The sorting function would need to be able to sort strings or numbers. You could declare two functions, but that's just a waste. You could pass the array in as a void type and then pass in a function pointer to compare the types, just like qsort() does. http://www.cplusplus.com/reference/clibrary/cstdlib/qsort.html

If you don't feel up to that, you could use something a bit simpler, such as:

void sort(void *data, int is_string, /*...*/) {
    /* ... */
    if(is_string) {
        if(strcmp(((char **)data)[0], ((char **)data)[1]) > 0) /* ... */
    }
    else {
        if(((int **)data)[0] > ((int **)data)[1]) /* ... */
    }
}

Or you could just write two separate functions . . . .

Something I gave away in that example: you use strcmp() to compare strings. It returns less than zero, equal to zero, or greater than zero, depending on whether its arguments were less than, equal to, or great than each other, respectively.

Is this right? (Line 75 or so)

if (element > element [left+1])   //???

{

   temp = element[left+1];   

   element[left+1] = element;   //???

   element = temp;   //???

}

element is the *address* of the array, a pointer to the array, and it's being compared with a value of the array??

You should be getting a warning about this from your compiler. You shouldn't compare an address with an integer or char value, nor should you try assigning the address of the array (which can't be assigned), the value of a char or array, or even another address.

oh my god!! i'm only a beginner at c and i don't know structs and unions, and i don't know the difference between int main and void main T_T that's why this is how i constructed my sorts.. T_T what is GCC anyway?? hmm.. but of course i do know how to understand the logic tha's my strong points.. sorry >_< if my logic is somehow difficult to understand T_T

Uhhm, i was able to fix the recent problems.. now, i think that my sorting algo is a bit off than what i imagined it, uhmm.. i haven't edited it though to integrate it with what you've written here.. but thanks! i think i'll try it later tonight or tomorrow, T__T 10 days before the judgement day..

#include"stdio.h"
#include"stdlib.h"

void read_list(int element[], int total,char choice)
{
  int ctr;

	if (choice == 'a' || choice == 'A')
	{
		for(ctr=0;ctr<total;ctr++)
		{
			printf("\n Enter Element [%d] ::" ,ctr);
			scanf("%s", &element[ctr]);
		}
	}
	if (choice == 'b' || choice == 'B')
	{
		for(ctr=0;ctr<total;ctr++)
		{
			printf("\n Enter Element [%d] ::" ,ctr);
			scanf("%d", &element[ctr]);
		}
	}
}


void print_list(int element[], int total,char choice)
{
  int ctr;

	if (choice == 'a' || choice == 'A')
	{
		for(ctr=0;ctr<total;ctr++)
			printf("  %c", element[ctr]);
	}
	if (choice == 'b' || choice == 'B')
	{
		for(ctr=0;ctr<total;ctr++)
			printf("  %d", element[ctr]);
	}
}


void insert_sort(int element[], int total,char choice)
{
	int pt, mid, left, right, ctr, ctr2, ctr3, temp;

  mid = (total+1)/2;
	pt = 0;

		for(ctr=total-1, ctr2=0; ctr>=mid && ctr2 <mid; ctr--, ctr2++)
		{

				if (element[ctr2] > element[ctr])
				{
					temp = element[ctr];
					element[ctr] = element[ctr2];
					element[ctr2] = temp;
				}

				for (left=0; left<=ctr2; left++)
				{
					if (element > element [left+1])
					{
						temp = element[left+1];
						element[left+1] = element;
						element = temp;
					}
				}

				for (right=total-1; right>=ctr; right--)
				{
					if (element[right-1] > element)
					{
						temp = element;
						element = element[right-1];
						element[right-1] = temp;
					}
				}

			printf("\n Go Pass %d :: ", ctr2);
			print_list(element,total,choice);

		}
}

void main()
{
  int element[20], total;
	char choice;
	clrscr();

	printf("Choose: A - Alphanumeric B - Numeric :: ");
	scanf("%c", &choice);

  printf("\n Enter Array Length :: ");
  scanf("%d", &total);

	read_list(element,total,choice);

	printf("\n The Array Elements are as follows :: ");
	print_list(element,total,choice);

	insert_sort(element,total,choice);

	printf("\n\n Final Answer :: ");
	print_list(element,total,choice);

	getch();
}

@Adak: I already mentioned that, though admittedly it was buried in my long post.

The main logical problem with your code still exists. Numbers aren't the same thing as strings. You can't have an array of ints and expect to be able to read strings or numbers into it; you can't have a function that sorts numbers and expect it to work with strings.

I think the easiest thing here would be for you to write a separate set of functions that handle strings. And remember, use strcmp() to compare strings.

what is GCC anyway??

It's a compiler.

This article has been dead for over six months. Start a new discussion instead.