10327 - Flip Sort
Moderator: Board moderators
10327
Plz help me to find the mistake
Here is my code
#include<stdio.h>
int main()
{
long int n,i,j,temp,a[100000],k,count=0,p;
while(scanf("%ld",&n)==1)
{
for(i=0;i<n;i++)
scanf("%ld",&a);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(a>a[j])
{
temp=a;
a=a[j];
a[j]=temp;
count++;
}
}
printf("Minimum exchange operations : %ld\n",count);
count=0;
}
return 0;
}
Here is my code
#include<stdio.h>
int main()
{
long int n,i,j,temp,a[100000],k,count=0,p;
while(scanf("%ld",&n)==1)
{
for(i=0;i<n;i++)
scanf("%ld",&a);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(a>a[j])
{
temp=a;
a=a[j];
a[j]=temp;
count++;
}
}
printf("Minimum exchange operations : %ld\n",count);
count=0;
}
return 0;
}
10327
This is my source code. Anyone can tell what's the problem in it?
/* p10327 */
/* Flip Sort */
#include<stdio.h>
void main()
{
long s[1002],n,i,j,x,m;
while(scanf("%ld",&n)!=EOF)
{
m=0;
for(i=0;i<n;i++)
scanf("%d",&s);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(s>s[j])
{
x=s;
s=s[j];
s[j]=x;
m++;
}
}
}
printf("Minimum exchange operations : %ld\n",m);
}
}
/* p10327 */
/* Flip Sort */
#include<stdio.h>
void main()
{
long s[1002],n,i,j,x,m;
while(scanf("%ld",&n)!=EOF)
{
m=0;
for(i=0;i<n;i++)
scanf("%d",&s);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(s>s[j])
{
x=s;
s=s[j];
s[j]=x;
m++;
}
}
}
printf("Minimum exchange operations : %ld\n",m);
}
}
-
- New poster
- Posts: 17
- Joined: Wed Mar 01, 2006 8:34 pm
- Location: 2nd floor
Rushow, you forgot the terms & conditions of the problem
think about second sample input-
so your algo is not flip (bubble) sort, actually replacement sort.
recently, our teacher taught us bubble sort, but you mistook. so bad.
let observe step by step, how flip sort will work
(currently comparing numbers will be shown in red)-
to clear more, u can also get help from: http://www.comp.nus.edu.sg/~stevenha/pr ... rting.html
// N.B. while writing code, use BBcode tags. visit BBcode Guide
but your algorithm didn't perform like this.only one operation ( Flip ) is available and that is you can exchange two adjacent terms
think about second sample input-
in your algo, first interchange occurs between 2 and 1, but 2 and 1 is not adjacent. always swap between two adjacent (consecutive positioned) number (when needed).2 3 1
so your algo is not flip (bubble) sort, actually replacement sort.
recently, our teacher taught us bubble sort, but you mistook. so bad.
let observe step by step, how flip sort will work
(currently comparing numbers will be shown in red)-
we swapped for 3 times. so ans=3.3 2 1 //the initial position of the array n=3 elements
3 2 1 //swap. after swapping: 2 3 1
2 3 1 //swap
2 1 3 //after n-1 comparison the largest element will in last position. so no eed to compare with it latter. again start from the beginning)
2 1 3 //swap
1 2 3 //after n-2 comparison the 2nd largest element will in 2nd last position. so no need to compare with it latter. there has only one element. so sorting was completed)
to clear more, u can also get help from: http://www.comp.nus.edu.sg/~stevenha/pr ... rting.html
// N.B. while writing code, use BBcode tags. visit BBcode Guide
[ Common thing of every man is that, everyone thinks that he is uncommon ]
-
- New poster
- Posts: 23
- Joined: Thu Jul 27, 2006 2:43 pm
- Location: University of Dhaka,Bangladesh
Hi Newton,you have to count only minimum exchange operations.
So,replace it .
By following
So,replace it .
Code: Select all
for(i = 0;i < num-1; i++)
{
for(j = i+1;j < num;j++)
{
if(array[i] > array[j])
{
cnt++;
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
Code: Select all
cnt = 0;
for(i = 0;i < num; i++)
{
for(j = i+1;j < num;j++)
{
if(array[i] > array[j])
cnt++;
}
}
-
- Experienced poster
- Posts: 109
- Joined: Sat Jun 23, 2007 9:53 pm
- Location: Brest, BELARUS
- Contact:
HOW can this code give WA?
I got already 7 (or like that) WAs on this easy prob.
I got already 7 (or like that) WAs on this easy prob.
Code: Select all
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
a: array [1..1000] of integer;
i,j,k,l,n: integer;
fl: boolean;
begin
while not eof do
begin
readln (n);
if eof then break;
for i:=1 to n do
read (a[i]);
k := 0;
fl := true;
while fl do
begin
fl := false;
for j:=1 to n-1 do
if a[j]>a[j+1] then
begin
inc(k);
fl := true;
l := a[j];
a[j] := a[j+1];
a[j+1] := l;
fl := true
end
end;
writeln ('Minimum exchange operations : ',k)
end;
end.
Now I lay me down to sleep...
my profile
my profile
10327....
I don't know why judge give me worng answer.... plezz help me.... This is too..... much crazy.
Code: Select all
removed
Last edited by Obaida on Tue Feb 24, 2009 2:49 pm, edited 1 time in total.
try_try_try_try_&&&_try@try.com
This may be the address of success.
This may be the address of success.
Cheque it
Thanks
Keep posting
sapnil
Code: Select all
Input:
5
5 1 3 5 1
output:
Minimum exchange operations : 5
Keep posting
sapnil
"Dream Is The Key To Success"
@@@ Jony @@@
@@@ Jony @@@
Thanks....
Thank You..... Got Ac....
try_try_try_try_&&&_try@try.com
This may be the address of success.
This may be the address of success.
10327 - Flip Sort --> more inputs
Input
5
6 2 1 5 4
8
3 5 8 2 1 9 7 0
2
0 0
12
0 1 2 5 4 1 2 0 5 0 7 9
30
21 5 6 5 8 452 12 5 96 21 21 5 6 5 8 452 12 5 96 21 21 5 6 5 8 452 12 5 96 21 21 5 6 5 8 452 12 5 96 21 21 5 6 5 8 452 12 5 96 21
output
Minimum exchange operations : 6
Minimum exchange operations : 16
Minimum exchange operations : 0
Minimum exchange operations : 19
Minimum exchange operations : 168
Minimum exchange operations : 67
5
6 2 1 5 4
8
3 5 8 2 1 9 7 0
2
0 0
12
0 1 2 5 4 1 2 0 5 0 7 9
30
21 5 6 5 8 452 12 5 96 21 21 5 6 5 8 452 12 5 96 21 21 5 6 5 8 452 12 5 96 21 21 5 6 5 8 452 12 5 96 21 21 5 6 5 8 452 12 5 96 21
output
Minimum exchange operations : 6
Minimum exchange operations : 16
Minimum exchange operations : 0
Minimum exchange operations : 19
Minimum exchange operations : 168
Minimum exchange operations : 67
Re: 10327 - Flip Sort --> more inputs
Do your code is accepted?? I am quite confused! Though i do not get ac, but your output have increased my confusion.carliros wrote: Input
Minimum exchange operations : 6
Minimum exchange operations : 16
Minimum exchange operations : 0
Minimum exchange operations : 19
Minimum exchange operations : 168
Minimum exchange operations : 67
Re: 10327 - Flip Sort
yes, my code was AC.
Re: 10327 - Flip Sort
this is my code. i got WA..
but what is wrong in this code?
#include<stdio.h>
int main()
{
long int n,a[1001];
while(scanf("%ld",&n)==1)
{
long int i,j,x=0,temp;
for(i=0;i<n;i++)
scanf("%ld",&a);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a>a[j])
{
temp=a;
a=a[j];
a[j]=temp;
x++;
}
printf("Minimum exchange operations : %ld\n",x);
}
return 0;
}
pls help me..
but what is wrong in this code?
#include<stdio.h>
int main()
{
long int n,a[1001];
while(scanf("%ld",&n)==1)
{
long int i,j,x=0,temp;
for(i=0;i<n;i++)
scanf("%ld",&a);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a>a[j])
{
temp=a;
a=a[j];
a[j]=temp;
x++;
}
printf("Minimum exchange operations : %ld\n",x);
}
return 0;
}
pls help me..
Re: 10327 - Flip Sort
You didn't need to do exchange but count how many exchange are available.
Ok, check this input,
Hope you find your mistake.
Ok, check this input,
Code: Select all
Input:
5
5 1 3 5 1
output:
Minimum exchange operations : 5
try_try_try_try_&&&_try@try.com
This may be the address of success.
This may be the address of success.