10327 - Flip Sort

All about problems in Volume CIII. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Re: 10327 - Flip Sort

Postby pok » Mon Nov 10, 2008 11:54 pm

thanks a lot..

i got it..

take care...bye ....
pok
New poster
 
Posts: 25
Joined: Sun Nov 09, 2008 11:04 pm

Re: 10327 - Flip Sort

Postby rubelreza » Fri Jul 24, 2009 3:40 pm

WHY I M GETTING WA???
CAN ANYONE TELL ME PLEASE..



#include<stdio.h>

int main(){

char s[1000];
int i,j,M=0,n,a,b;

while(scanf("%d\n",&n)==1){
M=0;
for(i=0;i<n;i++){
scanf("%d",&s[i]);}


for(b=0;b<n-1;b++){
for(j=0;j<n-1;j++){

if(s[j]>s[j+1])
{
a=s[j];
s[j]=s[j+1];
s[j+1]=a;
M++;
}}}
printf("Minimum exchange operations : %d\n",M);

}

return 0;
}
rubelreza
New poster
 
Posts: 2
Joined: Fri Jun 26, 2009 12:08 am

Re: 10327 - Flip Sort

Postby Obaida » Sat Jul 25, 2009 12:00 am

Your approach seems wrong to me.
Just do bubble sort and don't exchange the values just count how many ways. :)

And read the whole thread before posting. There are many discussion about it. :-?
>>Use code tags while posting your code. :D
try_try_try_try_&&&_try@try.com
This may be the address of success.
Obaida
A great helper
 
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10327 - Flip Sort

Postby tanjima09 » Wed Aug 12, 2009 10:12 am

#include <iostream>
#include <algorithm>
#include <algo.h>
using namespace std;

int main ()
{
int a[1002],n,i,count=0;

while(cin >> n)
{
if(n<=0)
break;
count=0;
for (i=0; i<n; i++)
cin >>a[i];


while (!is_sorted(a,a+n))
{
for (i=0; i<n; i++)
{
if (a[i]>a[i+1])
{
swap(a[i], a[i+1]);
count++;
}
}
}
cout<<"Minimum exchange operations : "<<count<<endl;
}
return 0;
}
tanjima09
New poster
 
Posts: 3
Joined: Mon Aug 10, 2009 7:04 pm

Re: 10327 - Flip Sort

Postby tanjima09 » Wed Aug 12, 2009 10:27 am

Code: Select all
#include <iostream>
#include <algorithm>
#include <algo.h>
using namespace std;

int main ()
{
   int a[1002],n,i,count=0;
      
   while(cin >> n)
   {
   if(n<=0)   
      break;
      
   count=0;
      for (i=0; i<n; i++)
         cin >>a[i];
      
      
   while (!is_sorted(a,a+n))
         {
         for (i=0; i<n; i++)   
            {
            if (a[i]>a[i+1])   
               {
               swap(a[i], a[i+1]);
               count++;
               }
            
            }
         
         }
         cout<<"Minimum exchange operations : "<<count<<endl;

   
   }            
   return 0;
}



i am getting a WA, i don't know what is the problem.. please help me with this....
tanjima09
New poster
 
Posts: 3
Joined: Mon Aug 10, 2009 7:04 pm

Re: 10327 - Flip Sort

Postby sazzadcsedu » Thu Aug 13, 2009 12:11 am

Your code is not correct.
Things to note:
for (i=0; i<n; i++)
cin >>a[i];

for (i=0; i<n; i++)
{
if (a[i]>a[i+1]) .....//problem here

look here you r comparing a[n-1]>a[n] when i=n-1(final pass).but what is the index of last element in a[]??is it n or n-1???
so write i<n-1 rather than n.

you have used #include <algo.h> i dont know what is this :-?
you have also used several built in function like is_sorted,swap.
i think u shouldn't use built in function for such a simple problem if you want to learn programing.
you should code this bubble sort manually.

here is ACC version of your code.try to understand the code.and write yourself. :D
Code: Select all
 removed.
Last edited by sazzadcsedu on Thu Aug 13, 2009 8:43 pm, edited 1 time in total.
sazzadcsedu
Experienced poster
 
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.

Re: 10327 - Flip Sort

Postby tanjima09 » Thu Aug 13, 2009 7:22 pm

thanks for ur reply.nw i understand where the problem was actually... yes i got an AC.. anywaz. i can do that by bubble sort. but i use those functions just because i want to. another thing is that <algo.h> is a very old name from STL HP implementation.thnx for helping :)
tanjima09
New poster
 
Posts: 3
Joined: Mon Aug 10, 2009 7:04 pm

Re: 10327 - Flip Sort

Postby asufcis » Sat Dec 24, 2011 6:25 pm

I'm gettin WA don'y know why ?

Code: Select all
#include <iostream>
#include <iomanip>

using namespace std;
int main ()
{
   //freopen("input.in", "r", stdin);

int n,x[1003];
while(cin>>n)
{
for(int i=0;i<n;i++)
{cin>>x[i];}

int count=0;
///////////ARRANGING ARRAY (SORTING) --->HERE ASCENDING FOR EXAMPLE//////////////
for(int i=0;i<n;i++)
   for(int j=i+1;j<n;j++)
{
   if (x[i]>x[j]) /////Swapping
   {swap(x[i],x[j]);
   count++;
   }
}
//////////////////////END OF SORTING///////////////////////////////////////

   cout<<"Minimum exchange operations : "<<count<<endl;
   
}



   return 0;
}
asufcis
New poster
 
Posts: 1
Joined: Wed Dec 21, 2011 3:37 am

Previous

Return to Volume CIII

Who is online

Users browsing this forum: No registered users and 0 guests