SwiftDeath
Programmer
I am creating a new sorting algorithm as a machine problem and i encounter difficulties.
The logic of my said the said algorithm is to create an array by inputing the total numbers to be accepted followed by the array variables and sorting it ascending or descending. The array would be sorted on both sides, through insertion sort.
So far, i didn't have any luck with it and my deadline is coming to a close. I really need anyone's help on this one i'm begging you.
Here is an example:
Array Total: 6
Ascending: 4 5 9 1 3 2
Compare the 1st and last number in ascending order. Swap if true.
1st Pass: 2 5 9 1 3 4
Increment both sides. this time the numbers to be compared are Group1(2,5) and Group2(3,4). Swap them according to their places.
2nd Pass: 2 3 9 1 4 5
Then the next pass, wherein you do the same procedure as number two. Group1(2,3,9) and Group2(1,4,5) Then swap.
3rd Pass: 2 3 9 1 4 5
The last pass where you arrange the whole array.
Last Pass: 1 2 3 4 5 9
If the amount of numbers to be arrange is odd. Then include the number in the middle to the left group and arrange it before doing the last pass.
Please everyone. Anyone. I really need your help on this.
#include"stdio.h"
#include"stdlib.h"
void read_list(int element[], int total)
{
int ctr;
for(ctr=0;ctr<total;ctr++)
{
printf("\n Enter Element [%d] ::" ,ctr);
scanf("%d", &element[ctr]);
}
}
void print_list(int element[], int total)
{
int ctr;
for(ctr=0;ctr<total;ctr++)
printf(" %d", element[ctr]);
}
void insert_sort(int element[], int total)
{
int mid, left, right, ctr, ctr2, ctr3, temp;
mid = total/2;
for(ctr=total-1; ctr>=mid; ctr--)
{
for(ctr2=0; ctr2<mid && element[ctr2] > element[ctr]; ctr2++)
{
temp = element[ctr];
element[ctr] = element[ctr2];
element[ctr2] = temp;
if ((ctr2 < mid) && (ctr >= mid))
{
if (element[ctr2] > element [ctr2+1])
{
temp = element[ctr2+1];
element[ctr2+1] = element[ctr2];
element[ctr2] = temp;
}
}
if ((ctr2 < mid) && (ctr >= mid))
{
if (element[ctr-1] > element[ctr])
{
temp = element[ctr];
element[ctr] = element[ctr-1];
element[ctr-1] = temp;
}
}
printf("\n Pass %d :: ", ctr2+1);
print_list(element,total);
}
}
}
void main()
{
int element[20], total;
clrscr();
printf("\n Enter Array Length :: ");
scanf("%d", &total);
read_list(element,total);
printf("\n The Array Elements are as follows :: ");
print_list(element,total);
insert_sort(element,total);
printf("\n Last Pass :: ");
print_list(element,total);
getch();
}
I really need you help on this one pls.
The logic of my said the said algorithm is to create an array by inputing the total numbers to be accepted followed by the array variables and sorting it ascending or descending. The array would be sorted on both sides, through insertion sort.
So far, i didn't have any luck with it and my deadline is coming to a close. I really need anyone's help on this one i'm begging you.
Here is an example:
Array Total: 6
Ascending: 4 5 9 1 3 2
Compare the 1st and last number in ascending order. Swap if true.
1st Pass: 2 5 9 1 3 4
Increment both sides. this time the numbers to be compared are Group1(2,5) and Group2(3,4). Swap them according to their places.
2nd Pass: 2 3 9 1 4 5
Then the next pass, wherein you do the same procedure as number two. Group1(2,3,9) and Group2(1,4,5) Then swap.
3rd Pass: 2 3 9 1 4 5
The last pass where you arrange the whole array.
Last Pass: 1 2 3 4 5 9
If the amount of numbers to be arrange is odd. Then include the number in the middle to the left group and arrange it before doing the last pass.
Please everyone. Anyone. I really need your help on this.
#include"stdio.h"
#include"stdlib.h"
void read_list(int element[], int total)
{
int ctr;
for(ctr=0;ctr<total;ctr++)
{
printf("\n Enter Element [%d] ::" ,ctr);
scanf("%d", &element[ctr]);
}
}
void print_list(int element[], int total)
{
int ctr;
for(ctr=0;ctr<total;ctr++)
printf(" %d", element[ctr]);
}
void insert_sort(int element[], int total)
{
int mid, left, right, ctr, ctr2, ctr3, temp;
mid = total/2;
for(ctr=total-1; ctr>=mid; ctr--)
{
for(ctr2=0; ctr2<mid && element[ctr2] > element[ctr]; ctr2++)
{
temp = element[ctr];
element[ctr] = element[ctr2];
element[ctr2] = temp;
if ((ctr2 < mid) && (ctr >= mid))
{
if (element[ctr2] > element [ctr2+1])
{
temp = element[ctr2+1];
element[ctr2+1] = element[ctr2];
element[ctr2] = temp;
}
}
if ((ctr2 < mid) && (ctr >= mid))
{
if (element[ctr-1] > element[ctr])
{
temp = element[ctr];
element[ctr] = element[ctr-1];
element[ctr-1] = temp;
}
}
printf("\n Pass %d :: ", ctr2+1);
print_list(element,total);
}
}
}
void main()
{
int element[20], total;
clrscr();
printf("\n Enter Array Length :: ");
scanf("%d", &total);
read_list(element,total);
printf("\n The Array Elements are as follows :: ");
print_list(element,total);
insert_sort(element,total);
printf("\n Last Pass :: ");
print_list(element,total);
getch();
}
I really need you help on this one pls.