Thursday, February 10, 2011

Assignment number 3: Sorting algorithm

Insertion Algorithm

            Insertion algorithm is a kind of sorting algorithm where the sorting is made one at a time starting from the left most part of an array. This kind of algorithm is very useful if you have only few data to be sorted. The advantage of this algorithm is it’s easy to implement and very efficient when you have a small data. It is very user friendly in the beginner sides. One of the disadvantages in insertion algorithm comes when the array is sorted from highest to lowest because when you sorted it from lowest to highest form, it will make the value of your array shifted in every data inside before it stops. For example, you have array with a value of 5 4 3 2 1. Before the array become 1 5 4 3 2, 5 will be compare to 4 then 4 will be the new data to be compare. Next, the 4 will be compared to 3 and so on until it will be 1 2 3 4 5.

Example code for Insertion:

By:” Nikolai Bezroukov, 1996 “

#include <stdio.h>
#include <stdlib.h>

typedef int 
itemtype;     // type of item to be sorted 
int 
total_comp=0; 
int 
total_moves=0;

void 
insertSort(
itemtype *a, // array to be sorted 
int n  // size of the array; note that (n-1) is the upper index of the array
) 
{
    
itemtype t;
    
int i, j;

   
/* Outer "boundary definition" loop */
    for 
(i = 1; i < n; i++) {
         if 
( a[i-1]<=a[i] ) { total_comp++; continue;}

        
t = a[i]; total_moves++;

        
/* inner loop: elements shifted down until insertion point found  */
        for 
(j = i-1; j >= 0; j--) {
        
     total_comp++;
             if 
( a[j] <= t ) { break; }    
           
  a[j+1] = a[j]; total_moves++;
        
}
        
/* insert */
        
a[j+1] = t; total_moves++;
    
}

}

Example Flow:


Selection Sort

            Selection sort is another sorting algorithm that is known by its simple structure and a few advantage compare to the complex sorting algorithm like quick sort. The Algorithm for this is first, you need to find the smallest value in your array. Next, swap the smallest value in your array to the beginning of your array. Next is to find the second smallest value and repeat the process all over again until the array will be sorted. Note that in every successful swapping, the iteration will be added with one until it will rich at the end of file.

Example code:

void selectionSort(int arr[], int n) {
      int i, j, minIndex, tmp;    
      for (i = 0; i < n - 1; i++) {
            minIndex = i;
            for (j = i + 1; j < n; j++)
                  if (arr[j] < arr[minIndex])
                        minIndex = j;
            if (minIndex != i) {
                  tmp = arr[i];
                  arr[i] = arr[minIndex];
                  arr[minIndex] = tmp;
            }
      }
}



Bubble Sort
            Bubble sort is a sorting algorithm that is very easy to understand but the implementation or the algorithm is not that good. The original name of this is “sorting by exchange” came from articles 1962 ACM Sorting conference .This algorithm performs (n) times of its algorithm just to sort them all and it’s not a good practice in programming. For example, you have an inputs 4 2 1 5 3. You need to have a variable x and variable y. x is equal to index zero and the y is equals to 1. Compare the two and if data y is greater than x, we need to swap the value then x and y plus one. Repeat the process until it reaches the end of an array. Repeat this process over and over again to get the sorted array.

Example code.

#include <stdio.h>
#include <iostream.h>

void bubbleSort(int *array,int length)//Bubble sort function
{
    int i,j;
    for(i=0;i<10;i++)
    {
        for(j=0;j<i;j++)
        {
            if(array[i]>array[j])
            {
                int temp=array[i]; //swap
                array[i]=array[j];
                array[j]=temp;
            }

        }

    }

}

void printElements(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<10;i++)
    cout<<array[i]<<endl;
}


void main()
{
    int a[]={9,6,5,23,2,6,2,7,1,8};   // array to sort
    bubbleSort(a,10);                 //call to bubble sort 
    printElements(a,10);               // print elements
}

Example Flow:

5 1 4 2 3
1 5 4 2 3
1 4 5 2 3
1 4 2 5 3
1 4 2 3 5
Next

1 4 2 3 5
1 2 4 3 5
1 2 3 4 5

Sometimes, the step will be repeated at (n) number of time where n=length of an array.

Shell Sort

            Shell sort is a sorting algorithm invented by Donald shell in 1959. Shell sort is a fast sorting algorithm and not bad but not that good for sorting large files. For me, shell sort is not a pure sorting algorithm but it’s only enhancing the speed of when you sort your file. It will not be effective if you will not use other sorting algorithm. The Algorithm of this is first you need to arrange your data from 1 column to N column. Sort your data from lowest to highest using another algorithm by rows. Arrange it again to make it one column and then divide it but this time, the column must be less than N column. If you are satisfied, you can sort it now by column  with a simple sorting algorithm but if your not satisfied. You can still repeat the process.

Example Code:

void shellsort (int[] a, int n)
{
    int i, j, k, h, v;
    int[] cols = {1391376, 463792, 198768, 86961, 33936, 13776, 4592,
                    1968, 861, 336, 112, 48, 21, 7, 3, 1}
    for (k=0; k<16; k++)
    {
        h=cols[k];
        for (i=h; i<n; i++)
        {
            v=a[i];
            j=i;
            while (j>=h && a[j-h]>v)
            {
                a[j]=a[j-h];
                j=j-h;
            }
            a[j]=v;
        }
    }
}

Example Flow :

 23 1 78 33 28 8 11 98 66 56 12 54 3 9 75 12 26 24 88

Arrange it in N column (where N is a programmer dependent)

23
1
78
33
28
8
11
98
66
56
12
54
3
9
75
12
26
24
88


Sort the data by column

8
1
3
9
28
12
11
24
33
56
12
26
78
66
75
23
54
98
88


Merge them all and make it one row and one column

8 1 3 9 28 12 11 24 33 56 12 26 78 66 75 23 54 98 88

Arrange it again in column less than n.

8
1
3
9
28
12
11
24
33
56
12
26
78
66
75
23
54
98
88



If you are satisfied with this, you can now perform a sorting algorithm to sort it by column and there you have it.

References:



Wednesday, February 2, 2011

assignment number 2

EMPERICAL ANALYSIS

As what i have search about the Empirical Analysis , I can only find samples of it but no specific meaning. Then I decide that just read the example given. Ive notice that Empirical Analysis is like an experiment or sometimes, it is a result of an observation and I think I'm right because according to Sir. W Hamilton [1913 Webster] -"the term empirical means simply what belongs to or is the product of experience or observation. ". Base on my reading, Empirical Analysis is all about what you have realize or learned base from your experiment or observation. Its like making a theory that you need to prove the you are right and for that, you need to experiment , observe, analyze and apply. When making Empirical Analysis, just expect that the new idea will be created and the new knowledge will be earned.

            ANALYSIS OF ALGORITHM

            We all know that Algorithm Analysis is the one who study about algorithm and according to what I've read , Analysis algorithm is a computer field that deals about the algorithm and understand the complexity of an algorithm. According to the article and base from my own understanding about that reading, we need to know how to analyze the algorithm so that we will know how much source is needed when it is executed. Algorithm will compute and estimate the shortest way in solving a specific problem and if you have a good algorithm, you can solve problem quickly but we need to remember always that in algorithm, we need to have the aspect of correctness and fast execution.
Sometimes algorithm failed when it comes to large input.

          One article that catches my attention is about the student  of Charles E. Leiserson. He taped the whole discussion and posted it in the need but unfortunately I don't have my headphone. He wrote there the important highlight about the lecture. C harles is one of the most popular author about algorithm. He started at  the state and theoretical study of algorithm.  He emphasize that in algorithm, performance is not always important. we need also to consider 

modularity,
correctness,
maintainability,
security,
functionality,
robustness,
user-friendliness,
programmer's time,
simplicity,
extensibility,
reliability, and
scalability.

And I really agree with his statement. For me, the Analysis of Algorithm is one of the most important components in building a better programmer because algorithm will define the quality of your work and the quality of your learning and somehow, am a little bit more interested about the topic and I want to know more.

            BIG-O NOTATION

Base on my reading, big o notation give information to the use by comparing two algorithms. It tells us about what will happen if we do some arrangement about our algorithm. It well tells us about the speed of execution that will result to the slower or higher speed of an algorithm run time.

According to my source, big o notations have functions.

1. O(1)-constant time
   -it requires a fixed number of steps and ignores the length like the push and pop in the link list.
2. O(n)-linear time
  -unlike O (1), O (n) requires a number of steps like traversing the link list until end.
3. O(n^2) - quadratic time
 -This is referring to the proportion of the size of n operator like the 2 dimensional arrays.
4. O(log n) - logarithmic time
-inserting or removing a binary node or searching fir a specific node.
5. O(n log n) - "n log n " time
-sorting algorithm and faster than O(log n).
6. O(a^n) (a > 1) - exponential time
-recursion algorithm like Fibonacci

Implementation

Suppose we have this code fragment

{
{Your statement......}                                                   O (1)

           
            for (;;)//loop statement
            {
                        {Statement}                                         O (n)
                        {Perform another loop}                        O (n^2)
            }

The execution time of this is 1+n+n^2=n2;