Home > OS >  this is a sorting array by frequency in c
this is a sorting array by frequency in c

Time:10-21

This code works fine for me, but when I upload it to the "Geeks For geeks" website, it gives me an error and says that this code does not work in many test cases, knowing that I tried it even at state 120..Is there a problem with this code?

#include <stdio.h>

int main() {
 //code
 int t ;
 int a=0;
 int count =0;
 int temp=0 ;
 int element ;
 int E_Temp=0;

 scanf("%d",&t);
 while(t--){
     int n ;
     scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i  ){
        scanf("%d",&arr[i]);
    }
    while(a<n){
        element=0;
        temp=0;
        for(int i=a;i<n;  i){//start from a no from 0 ***
            for(int j=a;j<n;  j){
                if(arr[i]==arr[j]){
                    count  ;
                }
            }
            
                if(count==temp){
                    if(arr[i]<element){
                    element=arr[i];
                    temp=count;
                    }
                    
                }
                else if(count>temp){
                    element=arr[i];
                    temp=count;//this because i will make the count"0" to make another process
                }
    
            
            count=0;
        }
        //sort the element ..
    
        for(int i=a;i<n;  i){//imprtant this constrans ....
         if (arr[i]!=element){//must check that not equal
         
          for(int j=i;j<n;  j){
                if (arr[j]==element){
                    E_Temp=arr[j];
                    arr[j]=arr[i];
                    arr[i]=E_Temp;
                    break;
                }
            }
         }  
         
        }
    
          a=a temp;
          
    }
     for(int i=0;i<n;i  ){
        printf("%d ",arr[i]);
        
    }
printf("\n");
 }
return 0 ;  
}

I tried it in all cases in the C language on gcc this the link https://practice.geeksforgeeks.org/problems/sorting-elements-of-an-array-by-frequency/0/?problemType=full&page=1&query=problemTypefullpage1#

CodePudding user response:

Variables a and count should be initialized to 0 before the nested loop while (a<n)

  • Related