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