Login
Immutable PageDiscussionInfoAttachments
Diff for "rei05/C言語"

MMA
Differences between revisions 28 and 29
Revision 28 as of 2012-06-01 16:47:36
Size: 1041
Editor: rei05
Comment:
Revision 29 as of 2012-06-01 16:54:19
Size: 1449
Editor: rei05
Comment:
Deletions are marked like this. Additions are marked like this.
Line 26: Line 26:
  * わざわざ1ページ作っての説明ありがとうございます。参考にさせていただきます。   * クイックソートで書いてみましたがTime Limit Exceeded。おかしな箇所を教えてください。-- [[rei05]] <<DateTime(2012-06-01T16:54:19+0900)>>
Line 29: Line 29:
#include<stdio.h>
int main(){
#include  <stdio.h>
Line 32: Line 31:
int n,i,j,k,l,temp;
scanf("%d",&n);
void Sort(int num[],int left,int right);
Line 35: Line 33:
int num[n];
for(i=0;i<n;i++){
  scanf("%d",&num[i]);
int main()
{
 int n,i;
 scanf("%d",&n);
 int num[n];

 for(i=0;i<n;i++)
  scanf("%d",&num[i]);

 Sort(num,0,n-1);

 for(i=0;i<n;i++)
  printf("%d ",num[i]);
 printf("\n");

 return(0);
Line 40: Line 51:
for(j=0;j<n;j++){
  for(k=0;k<j;k++){
    if(num[j]<num[k]){
      temp=num[j];
      num[j]=num[k];
      num[k]=temp;
    }
  }
}
void Sort(int num[],int left,int right)
{
 int i,j,temp;
 int center=(left+right)/2;
Line 50: Line 56:
for(l=0;l<n-1;l++){
  printf("%d ",num[l]);
}
printf("%d\n",num[n-1]);
 while(1){
  i=left;
  j=right;
Line 55: Line 60:
return(0);   while(num[i]<num[center])
   i++;
  while(num[center]<num[j])
   j--;
  if(i==j)
   break;
  if(num[i]==num[j]){
   i++;
   if(i==j)
    break;
  }

  temp=num[i];
  num[i]=num[j];
  num[j]=temp;
 }

 if(center-left>=2)
  Sort(num,left,center-1);
 if(right-center>=2)
  Sort(num,center+1,right);

C言語

AOJ解答

Help Me!

未解決

   1 #include  <stdio.h>
   2 
   3 void Sort(int num[],int left,int right); 
   4 
   5 int main()
   6 {
   7         int n,i;
   8         scanf("%d",&n);
   9         int num[n];
  10 
  11         for(i=0;i<n;i++)
  12                 scanf("%d",&num[i]);
  13 
  14         Sort(num,0,n-1);
  15 
  16         for(i=0;i<n;i++)
  17                 printf("%d ",num[i]);
  18         printf("\n");
  19 
  20         return(0);
  21 }
  22 
  23 void Sort(int num[],int left,int right)
  24 {
  25         int i,j,temp;
  26         int center=(left+right)/2;
  27 
  28         while(1){
  29                 i=left;
  30                 j=right;
  31 
  32                 while(num[i]<num[center])
  33                         i++;
  34                 while(num[center]<num[j])
  35                         j--;
  36                 if(i==j)
  37                         break;
  38                 if(num[i]==num[j]){
  39                         i++;
  40                         if(i==j)
  41                                 break;
  42                 }
  43 
  44                 temp=num[i];
  45                 num[i]=num[j];
  46                 num[j]=temp;
  47         }
  48 
  49         if(center-left>=2)
  50                 Sort(num,left,center-1);
  51         if(right-center>=2)
  52                 Sort(num,center+1,right);
  53 }

rei05/C言語 (last edited 2012-07-16 17:50:37 by nomeaning)