Thread: Insertion sort

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    1

    Insertion sort

    Hi,
    Im a total beginner and I have to do this for school. Spent so much time wondering whats wrong with this... Thanks
    No errors but doesnt work...

    Code:
    #include <stdio.h>
    
    int nactiZeSouboru (FILE* vstup, int pole[]){
      int i=0;
      while (fscanf(vstup,"%d",&pole[i])==1)
      {
        i++;
      }
      return i;
    }
    
    void InserttSortt(int pole[], int n)
    {
        int i, pom, j;
        for (i = 1; i < n; i++) {
            pom = pole[i];
            j = i;
     
            while (j >= 0 && pole[j-1] > pom) {
                pole[j] = pole[j-1];
                j = j - 1;
            }
            pole[j + 1] = pom;
        }
    }
    
    void zapisDoSouboru (FILE* vystup,int pole[],int n){
      
      for (int i=0;i<n;i++)
      {
        fprintf(vystup,"%d ",pole[i]);
      }
    }
    
    int main() {
    
      FILE* f=fopen("vstup.txt", "r");
      if(f==NULL){
        return -1;
      }
      FILE* g=fopen("vystup.txt", "w");
      if(g==NULL){
        return -1;
      }
     
     int i=0, n, vstup, vystup, pole[1000];
    
    nactiZeSouboru(f, pole);
    InserttSortt(pole, i);
    zapisDoSouboru(g, pole, n);
    
      fclose(f);
      fclose(g);
        return 0;
    }
    
    

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    210
    The first thing I see is that >= 0should just be > 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using insertion sort to sort a database by age
    By codemonkey2013 in forum C Programming
    Replies: 1
    Last Post: 04-29-2014, 05:05 PM
  2. insertion sort vs shell sort
    By johnmerlino in forum C Programming
    Replies: 34
    Last Post: 04-28-2014, 06:41 PM
  3. Insertion sort
    By UserName112 in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2011, 03:47 AM
  4. Replies: 1
    Last Post: 01-26-2010, 09:02 AM
  5. Insertion Sort Help
    By vgame64 in forum C++ Programming
    Replies: 2
    Last Post: 09-08-2006, 07:54 AM

Tags for this Thread