Shortcut for chapter specific information

Saturday, May 14, 2011

Bubble Sort 1

Bubble Sort 1. 
Finished my bubble sort.  I have written one while back when I was working in a open source project.   I always thought that I was a great programmer - the truth is I am just a super moron.   I never thought that there was such an art in sorting (not to say programming).   The intricacy of every technique is very interesting by its own sake.   At that time, I was just too entrenched in the area I was working on.

When I was asked to give a sorting implementation to a big firm.  I sent them a bubble sort.  Of course, there was a professional blunder.  If I had the training right now.  I will send at least a merge sort.   (I haven't mastered heap and quick yet. But wait and see, it won't be long.)
I guess this is why I want to implemented bubble anxiously.  I want to get over it.  Its understanding, just like binary insertion sort, is no longer crucial to modern algorithmic study.  But I always think you need to be wrong sometime to learn.  It would be a good thing for me to remember this valuable lesson from now on. 


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

No comments:

Post a Comment