Shortcut for chapter specific information

Showing posts with label selection sort C version. Show all posts
Showing posts with label selection sort C version. Show all posts

Tuesday, May 3, 2011

Daily Practice 20110503

Insertion Sort 61: Bad
Insertion Sort 62: Good
Selection Sort 24: Good
 
Though mentally fixed a bug in the selection sort.  I skipped a parsing error in insertion sort 61 --  Certainly not in the flow.  Feeling frustrated for various things in life. 
 
This is boredom.  In one way I should start at least one thing new today because it's hard to do something routine and hope that boredom doesn't kill me. 
 
I also want to remember how long I can keep writing correct code.  That gives an idea how long my concentration can hold.

Wednesday, April 27, 2011

Daily Practice 20110427(a)

Insertion sort 44 (C): Good
Selection sort 16 (C): Good


First time I got it right the first time.  Good. For another couple of days, I will start to add merge sort to my routine.
33_P

Tuesday, April 26, 2011

Expanded Further readings for insertion sort and selection sort

Further readings for insertion sort and selection sort

Insertion sort:
-sentinel version of straight insertion sort
-binary search
-binary insertion sort
-recursive version of insertion sort
-shell sort
-library sort
-tree sort(?)
-TAOCP Exercise in 5.2.1

Selection sort:
-stable version of selection sort.
-bidirectional version of selection sort.
-comparison with insertion sort.
-comparison with bubble sort
-recursive version of insertion sort
-cycle sort
-heap sort variants
     -tournament sort
     -smoothsort. 
-TAOCP Exercise in 5.23

Daily Practice 20110426(b)

Insertion sort 41 (C): Good
Selection sort 13 (C): Good
33_P

Daily Practice 20110426

Insertion sort 40 (C): Good
Selection sort 10,11,12 (C): 10 Bad, 11 Bad, 12 Good.  Why? 
10 used tmp rather than A in swapping - it's a good thing to always mind the variable you used.
11 used the assign A[j] to min instead of j.  The caused the whole program looks like it is working.  This is a subtle bug if the value of A is closed to sort.

My problem of selection sort is that I hadn't gone through the what if analysis of the whole algorithm.  I should try it some time.

33_P

Monday, April 25, 2011

Daily Practice 20110425

Insertion sort: 38th and 39th writings (C) : 38th - disaster, got syntax wrong for initializing an array and assigned a wrong variable to k.  Make it your habit to think the index order.  39th - get it right.

Selection sort: 8th writing and 9th writings (C) : 8th - wrong, change the sign of comparison.  So it sorts the other way.  9th is good.

Found myself not very focused today.

33_P

Sunday, April 24, 2011

Daily Practice 20110424b

insertion sort: 38th and 39th writings in C :38th missed a ;, 39th is good.  At 11:35 p.m., I was feeling tired and certainly lost focus.  But this is good time to practice making myself focused.
selection sort: 7th writing in C: Good.

Each take around 3 mins.

33_P

Daily Practice 20110424

insertion sort: 37th writing in C : Good ~3mins.
selection sort : 5th and 6th writing in C : 5th is bad but 6th is good.  5th has issue because of swapping was slightly wrong.   Now I know a non-insertion sort error pattern in my head. ~5 mins.

I am taking approximately 3 mins in insertion sort and 5 mins in selection sort.  It is certainly something I can improve.   My first speed goal is to write them under 60 seconds.

33_P

2nd - 4th writing of selection sort (C)

#include <stdio.h>
#define    N 10
int main(int c,char* v[])
{
  int A[N] = {3, 5, 7, 1, 2, 6, 8, 9, 10, 0};
  int i,j,imin,t;
  for(j=0;j<N-1;j++){
    imin = j;
    for(i=j+1;i<N;i++)
      if(A[i]<A[imin])
    imin=i;
    if(j!=imin){
      t=A[j];
      A[j]=A[imin];
      A[imin]=t;
    }
  }
  for(i=0;i<N;i++)
    printf("%d ",A[i]);
  printf("\n");
  return 0;
}

This is the version I decided to use at the end.  My 2nd write blews. Why? I failed to apply my mental checking *before* the code is compiled.  I am still too impatient.  My 3rd and 4th are good. 

Lesson: if you already have an algorithm in your head which is refined, when learning a new algorithm, try to apply things you learn from these algorithms.  e.g. I know insertion sort.  Why didn't I use my technique in insertion sort to check my selection sort?
33_P

Wednesday, April 20, 2011

First writing of Selection sort (TCRC Pseudocode)

I started to feel mildy confident about insertion sort.  So I move on to write my first version of selection sort. (i.e. Also solved 2.2.2, need to put in on paper).

My answer is two-line longer compare to solution I can found on-line.  Too bad, let's refine it later.

33_P