Sunday, March 22, 2009

♥ SORTING ALGORITMS ♥

  • Bubble sort- is a simple sorting alrorithms . It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list.

Examples:

First Pass:
( 5 1 4 2 8 ) ---> ( 1 5 4 2 8 ) Here, algorithm compares the first two elements,
and swaps them.
( 1 5 4 2 8 ) ---> ( 1 4 5 2 8 )
( 1 4 5 2 8 ) ---> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) ---> ( 1 4 2 5 8 ) Now, since these elements are already in order,
algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) ---> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) ---> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ---> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ---> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed.
Algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) ---> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ---> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ---> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ---> ( 1 2 4 5 8 )
Finally, the array is sorted, and the algorithm can terminate.

[wiki] http://en.wikipedia.org/wiki/Bubble_sort



  • Insertion sort- is a simple sorting algorithm that is relatively efficient for small lists and mostly-sorted lists, and often is used as part of more sophisticated algorithms. It works by taking elements from the list one by one and inserting them in their correct position into a new sorted list. In arrays, the new list and the remaining elements can share the array's space, but insertion is expensive, requiring shifting all following elements over by one.

Examples
http://web.syr.edu/%7Etjhaskel/hmk8a.gif

400 x 292 - 11k - gif - web.syr.edu/%7Etjhaskel/hmk8a.gif

Below is the image at: web.syr.edu/%7Etjhaskel/



[wiki] http://en.wikipedia.org/wiki/Sorting_algorithm#Insertion_sort

  • Shell sort- was invented by Donald Shell in 1959. It improves upon bubble sort and insertion sort by moving out of order elements more than one position at a time. One implementation can be described as arranging the data sequence in a two-dimensional array and then sorting the columns of the array using insertion sort. Although this method is inefficient for large data sets, it is one of the fastest algorithms for sorting small numbers of elements (sets with fewer than 1000 or so elements). Another advantage of this algorithm is that it requires relatively small amounts of memory.

Example: Let 3 7 9 0 5 1 6 8 4 2 0 6 1 5 7 3 4 9 8 2 be the data sequence to be sorted. First, it is arranged in an array with 7 columns (left), then the columns are sorted (right):

3790516
8420615
734982


arrow

3320515
7440616
879982

Data elements 8 and 9 have now already come to the end of the sequence, but a small element (2) is also still there. In the next step, the sequence is arranged in 3 columns, which are again sorted:

332
051
574
406
168
799
82




arrow

001
122
334
456
568
779
89

http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/shell/shellen.htm
[wiki] http://en.wikipedia.org/wiki/Sorting_algorithm#Shell_sort


  • Merge sort- takes advantage of the ease of merging already sorted lists into a new sorted list. It starts by comparing every two elements (i.e., 1 with 2, then 3 with 4...) and swapping them if the first should come after the second. It then merges each of the resulting lists of two into lists of four, then merges those lists of four, and so on; until at last two lists are merged into the final sorted list. Of the algorithms described here, this is the first that scales well to very large lists, because its worst-case running time is O(n log n).

Examples:
http://upload.wikimedia.org/wikipedia/en/thumb/e/e6/Merge_sort_algorithm_diagram.svg/300px-Merge_sort_algorithm_diagram.svg.png

[wiki] 300 x 289 - 22k - png - upload.wikimedia.org/wikipedia/en/thumb/e/e6/...

Below is the image at: en.wikipedia.org/wiki/Merge_sort

[wiki] http://en.wikipedia.org/wiki/Sorting_algorithm#Merge_sort


  • Heapsort- is a much more efficient version of selection sort. It also works by determining the largest (or smallest) element of the list, placing that at the end (or beginning) of the list, then continuing with the rest of the list, but accomplishes this task efficiently by using a data structure called aheap, a special type of binary tree. Once the data list has been made into a heap, the root node is guaranteed to be the largest element. When it is removed and placed at the end of the list, the heap is rearranged so the largest element remaining moves to the root. Using the heap, finding the next largest element takes O(log n) time, instead of O(n) for a linear scan as in simple selection sort. This allows Heapsort to run in O(n log n) time.

Example:

http://scienceblogs.com/goodmath/heap-array-example.png

362 x 361 - 20k - png - scienceblogs.com/goodmath/heap-array-example.png

Below is the image at: scienceblogs.com/goodmath/2008/04/implementin...



[wiki] http://en.wikipedia.org/wiki/Sorting_algorithm#Heapsort

  • Quicksort- is a divide and conquer algorithm which relies on a partition operation: to partition an array, we choose an element, called a pivot, move all smaller elements before the pivot, and move all greater elements after it. This can be done efficiently in linear time andin-place We then recursively sort the lesser and greater sublists. Efficient implementations of quicksort (with in-place partitioning) are typically unstable sorts and somewhat complex, but are among the fastest sorting algorithms in practice.

A Step Through Example

  1. This is the initial array that you are starting the sort with


  2. The array is pivoted about its first element p =3


  3. Find first element larger than pivot (underlined) and the last element not larger than pivot (italicised)


  4. Swap those elements


  5. Scan again in both directions


  6. Swap


  7. Scan


  8. The pointers have crossed: swap pivot with italicised.


  9. Pivoting is now complete. Recursively sort subarrays on each side of pivot.

http://www.cs.usask.ca/content/resources/csconcepts/1998_4/quick/2-4.html
[wiki] http://en.wikipedia.org/wiki/Sorting_algorithm#Bubble_sort

  • Bucket sort, or bin sort, is a sorting algorithm that works by partitioning an array into a number of bucket . Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a cousin of radix sort in the most to least significant digit flavour. Since bucket sort is not a comparison sort, the Ω(n log n) lower bound is inapplicable. Estimates involve the number of buckets.
http://upload.wikimedia.org/wikipedia/commons/3/39/Bucket_sort_2.png

311 x 132 - 5k - png - upload.wikimedia.org/.../3/39/Bucket_sort_2.png

Below is the image at: commons.wikimedia.org/wiki/File:Bucket_sort_2.png


[wiki] http://en.wikipedia.org/wiki/Bucket_sort

Thursday, March 12, 2009

Implementation of Queue

/* Programmer’s name: Nemily Bayacag
Name of Program: Queue implementation
Date Started: March 12, 2009
Date Finished : March 13, 2009
Instructor : Mr. Dony Dongiapon
Course: IT 123: Data Structures
Objective: To be able to make a program that we can implement a queue data structure in a linked list.
*/


Concept: List of employees in a given company

//class constructor
class Queue{
public int employeenum;
public String firstname;
public String Lastname;
public char Middlename;
public Queue next;


public Queue (int Enum, String Fname, String Lname, char M; )
{

emlopyeenum=Enum;
firstname=Fname;
lastname=Lname;
middlename=M;
}


//displaying the elements on the list
public void displayQueue()
{
System.out.print(employeename +” “ + firstname +” “ +” “+middlename+ “ “ +: + lastname)
}
}


/*a separate class which contains the ,methods that would be used in implementing the program */
class QueueList
private Queue first;
private Queue last;
public QueueList()
{
first=null;
last=null;
}


//checking if the queue has elements
public Boolean isEmpty()
{
return (first==null);
}

//inserting an element on the queue
public void Enqueue(int Enum, String Fname, String Lname, char M,)

{
Queue newQueue= new Queue (int Enum, String Fname, String Lname, char M,)

if( isEmpty())
last = newQueue;
newQueue.next=first;
first=newQueue;
}


//deleting an element on the queue
public void Dequeue (int Enum)
{
Queue newQueue=new Queue (int Enum, String Fname, String Lname, char M)

int temp=first.entrynum;
if (first.next==null)
last=null;
first=first.next;
return temp


}
}


public class MainClass {
public static void main(String[] args) {
LinkQueue theQueue = new LinkQueue();
theQueue.enqueue(1, “Nemilyn”, “Bayacag”, ‘M’ )

theQueue.enqueue(2, “Lovely”, “Bajar”, ‘C’ );
System.out.println(theQueue);

theQueue.enqueue(3,“Nellen”, “Ortiz”, ‘C’ )
System.out.println(theQueue)



theQueue.dequeue(3);

System.out.println(theQueue);



System.out.println(theQueue);
}
}