Thread: Selection Sort

  1. #16
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25


    Last night while you were helping me my code was working (i realize this isn't a selection sort but it's not really a big deal)

    here is the output

    Code:
     Spots in array:
     5
    
     Spot 1 in array : 5
    
     Spot 2 in array : 4
    
     Spot 3 in array : 3
    
     Spot 4 in array : 2
    
     Spot 5 in array : 1
     if 5 > 5
     if 5 > 4
    then swap them if 5 > 3
    then swap them if 5 > 2
    then swap them if 5 > 1
    then swap them if 4 > 3
    then swap them if 4 > 2
    then swap them if 4 > 1
    then swap them if 4 > 5
     if 2 > 1
    then swap them if 2 > 4
     if 4 > 5
     if 2 > 4
     if 4 > 5
     if 4 > 5
     
     
     iSort
    3
    1
    2
    4
    5
    Press any key to continue . . .

    Why did she skip 3? For the life of me I can not figure this out.

  2. #17
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25
    Nevermind, I have been playing with it and changed the inner loop to go other way..

    Code:
    for (j=i; j >= 0; j--)
    It works guys!

    What i don't understand is why this works. I can't visually see our j loop starting at i (which is zero) and moving downwards (j--) to sort. If you guys wouldn't mind giving me a metaphor I would really appreciate it. Thank you.

  3. #18
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25
    sorry to keep posting it up but I understand now! (Programming is so awesome)

  4. #19
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Bubblesort?
    Selection sort?
    What it is?
    Well it seems to be something you made up, which is good from the point of view that you thought how it worked.But in more difficult problems it is good to see the pseudocode of an already existing algorithm.This will help you do your work and moreover , do it efficiently

    Your algorithm is taking time complexity of O(n²) ,which can be reduced enough

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You should know that sorting is one of the most common computer tasks, and studies have shown computers spend a lot of their time, doing it. For that reason, it has been studied in depth.

    Each sorting algorithm is different: some are fast with random data, (Quicksort), some are fast with nearly sorted data (Insertion), some sorts don't care (Heapsort). Some make very few swaps (Selection), one makes the fewest possible number of sorts (Perfect), some are in place (Quicksort, Shell sort), others use a large amount of extra memory (Merge sort).

    For small sets of data (and "small" changes with computer hardware):
    Bubble, Insertion, Substitution - all easy to code

    For medium sets of data:
    Insertion, Shell, Bucket, QuickShort, qsort()

    For large sets of data:
    Heapsort, Quicksort, Mergesort, Radix, Bucket, qsort()

    qsort() is part of the C standard.
    This is the shortest version of Quicksort - called QuickShort.
    Code:
    void quickShort(int A[], int l, int u) {
      int i, m;
      if(l >= u) return;
      m = l;
      for(i = l+1; i<=u; i++) {
        if(A[i] < A[l])
          swap(A,++m, i);
      }
      swap(A, l, m);
      quickShort(A, l, m-1);
      quickShort(A, m+1, u);
    }
    QuickShort isn't a speed demon, but it is a solid mid-performer.

    It's a good idea to become familiar with one or two sorting algorithms from at least two of the categories. Bubble or Substitution sort are very easy for the smallest sorting jobs, but I'm going to recommend Insertion sort - it's clearly faster, and no harder to code.

    For large jobs, I tend to favor my fastest sorter, an Optimized Quicksort (Quicksort + Insertion for the smaller sub arrays), and Mergesort. They're real workhorses for huge sorting jobs.

  6. #21
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25
    I guess this is more or less a bubble sort that moves int's backwards..i realize its not a selection sort, why its not a selection sort, and have learned a lot so its a success, now I will be trying to incorporate taking data out of a file, sorting it, and writing it into a different file from the command line.

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Here's your original program, with a Bubble sort. The original one failed because j was starting it's loop, at i. J needed to start at 0 so it could "reach back" to the lower indices in the array, over and over. It only has to go to the top index once, but it always has to check the lower values in the array's lower indices.
    Code:
    #include <stdio.h>
    #include <conio.h>
     
    void swap (int *a, int *b) 
    {
       int temp;
       temp = *a;
       *a = *b;
       *b = temp;
    }
    
    void bubbleSort(int a[], int x)  
    {
       int i, j,n = x;
       for (i = 0; i < x; i++)
       {
          for(j=0; j < n; j++)
          { 
             if (a[j] > a[j+1]) 
             {
                //printf("then swap them");
                swap(&a[j], &a[j+1]);
             }
          }
          --n;
       }
    }
    
    int main()  
    {  
      
       int a[100],x,i;  
      
       printf(" Values in array before sorting: \n\n");  
          //scanf("%d",&x);  
          
       x=100;
       for( i=0;i<x;i++)  
       { 
          a[i]= x -i;
    
                //printf("\n\n Spot %d in array : ",i+1);  
                //  scanf("%d",&a[i]);  
       }  
       
       for( i=0;i<x;i++) {
          printf("%4d ",a[i]);  
          if((i+1)%10==0)
             printf("\n");
    
       }
       bubbleSort(a,x);
      
       printf("\n\n\n\n\n iSort - Values in array after sorting: \n\n");  
       for( i=0;i<x;i++)
       {
          printf("%4d ",a[i]);
          if((i+1)%10==0)
             printf("\n");
                
       }
      
       printf("\n");    
       //system("Pause");
       return 0;
    }
    Last edited by Adak; 11-09-2012 at 01:24 PM.

  8. #23
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25
    Thank for your code adak, however it only runs what I depict as a printing a ton of ram locations; I am having the same problem.

    Code:
    #include <stdio.h>
    #include <conio.h>
     
     
    void swap (int *a, int *b) 
    {
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
    }
    int sort(int a[], int x)
    {
    int i, j;
     for (i = -1; i < x-1; i++)
     {
      
     
      for(j=i; j >= 0; j--)
     
      { 
            printf(" if %d > %d \n",a[j], a[j+1]);
       if (a[j] > a[j+1])
       {
            printf("\n\nthen swap them\n");
       swap(&a[j], &a[j+1]);
       }
      }
     }
     
    }
    int main()  
      {  
      
          int a[100], x, i;  
      
            
          printf(" Spots in array: \n ");  
          scanf("%d",&x);  
      
          for( i=0;i<x;i++)  
      { 
          printf("\n\n Spot %d in array : ",i+1);  
          
          scanf("%d",&a[i]);  
            }  
      
           sort(&a[i],x);
      
           printf("\n\n\n\n\n iSort \n\n");  
           for( i=0;i<x;i++)
           {
           printf("%d at %d\n",a[i], &a[i]);  
           }
          
      
      system("Pause");
      }
    I am running into a similiar problem with ram locations. I added some printfs to the source in order to show myself and anyone troubleshooting with me whats going on here. To my knowledge, my code is using the for loops to compare not the value of the array but the location of the array. I have done a reasonable ammount of research on pointers and arrays and here is what I understand. (I'm not in school for this I'm just trying to attain the best ammount of knowledge from the resources I have):

    Arrays (such as a[5]) is a pointer to a location which has 5 spots within its grid that contain the list of my numbers I am trying to sort. So when a[1] is larger than a[2] it should move a[1] into temp, than a[2] into its spot, a[1] gets ready to be compared again in the temp spot.
    Last edited by Andersonsacanno; 11-13-2012 at 07:30 AM.

  9. #24
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Why you call it like this?
    Code:
    sort(&a[i],x);
    Just pass the array as first argument.
    Code:
    sort(a,x);

  10. #25
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25
    Aha! Back in business!

    So i was telling it to sort the locations not the values?

  11. #26
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25
    Code:
    #include <stdio.h>
    #include <conio.h>
     
     
    void swap (int *a, int *b) 
    {
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
    }
    int sort(int a[], int x)
    {
    int i, j;
     for (i = -1; i < x-1; i++)
     {
      
     
      for(j=i; j >= 0; j--)
     
      { 
            printf(" if %d > %d \n",a[j], a[j+1]);
       if (a[j] > a[j+1])
       {
            printf("\n\nthen swap them\n");
       swap(&a[j], &a[j+1]);
       }
      }
     }
     
    }
    int main()  
      {  
      
          int a[100], x, i;  
      
            
          printf(" Spots in array: \n ");  
          scanf("%d",&x);  
      
          for( i=0;i<x;i++)  
      { 
          printf("\n\n Spot %d in array : ",i+1);  
          
          scanf("%d",&a[i]);  
            }  
      
           sort(a ,x);
      
           printf("\n\n\n\n\n iSort \n\n");  
           for( i=0;i<x;i++)
           {
           printf("%d at %d\n",a[i], &a[i]);  
           }
           
           FILE *fp;
           fp=fopen("c:\\test.txt", "wb");
           fwrite(a, sizeof(a[0]), sizeof(a)/sizeof(a[0]), fp);
    
      
      system("Pause");
      }
    I am now trying to send my array into a file on C:

    Using fwrite in this manner should write from memory into the file according the the I/O tutorial.

    this however is my output:

    Code:
                      h            I H %    ˆ P       P s     P ›  ›  ›  ›   Øþ( Õq)w   þÿÿÿ   î<%w   I H   p  %        î<%w       P `     h ˆ ð                      ¼ý( Ä } øþ( Õq)wÎ1þÿÿÿš8%w’4%w    h h         ` ÿ( ͘Óu       Ú˜Óu÷•õ            ÿ^n€þÿÿÿù­ÓuC®ÓuÐþ(    Äÿ( ÕŒÕuhn€þÿÿÿÚ˜Óuú Ôuh x 8ÿ( ŸÔu        pÿ( ä   Ü   @-} 7   

  12. #27
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    ok.The b specifier is for binary.You do not want that.
    Open the file with "w"
    Then see the ref for fwrite
    Set as third argument 100.More clear for sure.
    Then set as second argument sizeof(int), again for readability.

  13. #28
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25
    Judging from what you've said and the fwrite source I tried these:

    Code:
    FILE *fp;
           fp=fopen("c:\\test.txt", "w");
           fwrite(a, sizeof(100), 100, fp);
           fclose (fp);

    Code:
    FILE *fp;
          fp=fopen("c:\\test.txt", "w");
          fwrite(a, 100, sizeof(a[100]), fp);
          fclose (fp);
    I'm quite confused as to where I'm wrong, it seems as if this should work.

  14. #29
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by std10093 View Post
    Then set as second argument sizeof(int)
    I have to write something in order to let me post.

  15. #30
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25
    fwrite sucks < fprintf rocks

    Code:
    #include <stdio.h>
    #include <conio.h>
     
     
    void swap (int *a, int *b) 
    {
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
    }
    int sort(int a[], int x)
    {
    int i, j;
     for (i = -1; i < x-1; i++)
     {
      
     
      for(j=i; j >= 0; j--)
     
      { 
            printf(" if %d > %d \n",a[j], a[j+1]);
       if (a[j] > a[j+1])
       {
            printf("\n\nthen swap them\n");
       swap(&a[j], &a[j+1]);
       }
      }
     }
     
    }
    int main()  
      {  
      
          int a[100], x, i;  
          FILE *
          
          fp=fopen("c:\\users\\hello\\desktop\\test.txt", "a+");
      
            
          printf(" Spots in array: \n ");  
          scanf("%d",&x);  
      
          for( i=0;i<x;i++)  
      { 
          printf("\n\n Spot %d in array : ",i+1);  
          
          scanf("%d",&a[i]);  
            }  
      
           sort(a ,x);
      
           printf("\n\n\n\n\n iSort \n\n");  
           for( i=0;i<x;i++)
           {
           printf("%d at %d\n",a[i], &a[i]);  
           fprintf (fp, "%d, ", a[i]);
           }
           
      
         
          
           
           
          
           system("Pause");
           
           
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Selection Sort
    By Drew in forum C++ Programming
    Replies: 16
    Last Post: 09-18-2003, 07:19 AM
  2. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 02:24 PM
  3. Selection Sort
    By volk in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2003, 06:54 AM
  4. Selection Sort
    By Bleueyes515 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2002, 08:33 PM
  5. Selection-sort
    By Nutshell in forum C Programming
    Replies: 10
    Last Post: 01-11-2002, 04:32 AM