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);
}
}

Tuesday, February 24, 2009

Queue

Definition:

According to wikipidea:



  • A queue is a particular kind of collection in which the entities in the collection are kept in order and the principal operations

  • collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position.

  • FIFO (First In First Out)

[wiki]http://en.wikipedia.org/wiki/Queue_(data_structure)


Illustrations:
















[350 x 340 - 40k - jpg - www.ac-nancy-metz.fr/.../Henry/bus-queue.jpg ]


Below is the image at: www.ac-nancy-metz.fr/.../anglais/Henry/ville.htm

Monday, February 16, 2009

Implementation of Stack

//To implement an array in a certain stacks.

class Link {
public int iData=0;

public Link(int iData,){

iData=id;
}

public void displayLink()
{
System.out.println(iData+":" );
}
}

class StackList{
private Link first;

public StackList(){
first=null;
}

public boolean isEmpty() {
return (first == null);
}

public void insertFirst( int id) {
Link newLink = new Link( id);
newLink.next = first;
first = newLink;
}

public Link deleteFirst()
{
Link temp=first;
return temp;
}

public Link pick()
{
Link temp=first;
return temp;
}

public void displayList
{
System.out.print("Elements on stack: ");
Link temp=first;
while(temp!=null)
{
temp.displayList();
}

System.out.println(" ");
}
}

class StackListApp
{
public static void main (String[]args)
{
StackList theList=new StackList();

theList.insertFirst(23);
theList.insertFirst(13);
theList.insertFirst(22);

theList.deleteFirst();

theList.displayList();
}
}

Double Ended Linked List

♥ Definition

According to our previous topic that our instructor introduced it double-ended linked list, it was an addition reference that point to the last linked list and first linked list.

B. Illustration:


[Illustration given by of our instructor]

Sunday, February 15, 2009

Implementation of Double Ended Link List

// To implement a double ended link list.

//constructor
class Link {


public int iData;
public double dData:
public Link next;

public Link(int id,double dd) {

iData = id;
dData=dd;

}

public void displayLink(){
System.out.print("{"+iData+","dData+"}");

}
}
class FirstLastList {

private Link first;
private Link last;

public FirstLastList() {

first = null;
last = null;

}

public boolean isEmpty() {

return (first == null);

}
// inserting a node in first element

public void insertFirst(int id,double dd) {

Link newLink = new Link(id,dd);

if (isEmpty ())

last = newLink;
newLink.next = first;
first = newLink;

}
// inserting a node in a last element

public void insertLast(int id,double dd) {

Link newLink = new Link(id,dd);

if (isEmpty())
first = newLink;

else

last.next = newLink;
last = newLink;
}

// delete the first node in the list
public Link deleteFirst(int id,double dd) {
int temp = first.iData;
if (first.next == null)
last = null;
first = first.next;
return temp;
}

// delete the last node in a list
public Link deleteLast(int id, double dd){
int temp=last.iData;
if(last.next==null)
first=null;
last=last.next;
return temp;
}
// display an element in a list
public void displayList(){
System.out.print("List(first-->Last);");
Link current=first;
while(current!=null){
current.displayLink();
current=current.next;
}

}
System.out.println(" ");
}

}

//main method to apply
public class FirstLastApp{

public static void main(String[] args) {

FirstLastList theList = new FirstLastList();

theList.insertFirst(19,1.90);
theList.insertFirst(13,1.9);
theList.insertFirst(25,5.62);
theList.insertLast(24,6.8);
theList.insertLast(23,3.79);
theList.insertLast(56,2.25);

System.out.println(theList);

theList.deleteFirst();

theList.deleteFirst();

System.out.println(theList);
}

}

Doubly Linked List

♥ Definition/Concept


According to Wikipedia:


A more sophisticated kind of linked list is a doubly-linked list or two-way lined list, that every each node has two links one points to the previous node or points to a null value or empty list if it is the first node, and one points to the next, or points to a null value or empty list if it is the final node.[wiki]

A double linked list is a linked list, in which each node contains a pointer to a next node and a pointer to a previous element in the list.[google]


♣ As I understand of doubly linked list it has a node that points in the next node and the other points in the previous node, to access with next and previous node.



Illustrations


Doubly-linked-list.svg‎ (SVG file, nominally 610 × 41 pixels, file size: 23 KB)
[This is a file from the Wikimedia Commons]

♥ Implementation

// To implement a doubly linked list.

//constructor
class Link{

public int iData;
public Link next;
public Link previous;
public Link(int id) {

iData = id;
}

// display element in a list
public void displayList()
{
return "{" + iData + "} ";
}
}

class DoublyLinkedList {

private Link first;
private Link last;

public DoublyLinkedList()
{
first = null;
last = null;
}

public boolean isEmpty()
{
return first == null;
}

//insertion of first element
public void insertFirst(int dd)
{
Link newLink = new Link(dd);

if (isEmpty())
{
last = newLink;
} else {
first.previous = newLink;
}
newLink.next = first;
first = newLink;
}

//insertion of last element
public void insertLast(int dd)
{
Link newLink = new Link(dd);
if (isEmpty())
{
first = newLink;
}else{
last.next = newLink;
newLink.previous = last;
}
last = newLink;
}

//deletion of first element
public Link deleteFirst()
{
Link temp = first;
if(first.next == null)
{
last = null;
} else {
first.next.previous = null;
}
first = first.next;
return temp;
}

//deletion of last element
public Link deleteLast()
{
Link temp = last;
if(first.next == null)
{
first = null;
} else {
last.previous.next = null;
}
last = last.previous;
return temp;
}

public boolean insertAfter(int key, int dd)
{
Link current = first;
while (current.iData != key)
{
current = current.next;
if (current == null)
{
return false;
}
}
Link newLink = new Link(dd);
if (current == last)
{
newLink.next = null;
last = newLink;
} else {
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true;


}

public Link deleteKey(int key)
{
Link current = first;
while (current.iData != key)
{
current = current.next;
if (current == null)
return null;
}
if (current == first)
{
first = current.next;
} else {
current.previous.next = current.next;
}
if (current == last)
{
last = current.previous;
} else {
current.next.previous = current.previous;
}
return current;
}
}

// the main method to apply
public class DoublyLinkedApp
{
public static void main(String[] args)
{
DoublyLinkedList theList = new DoublyLinkedList();

theList.insertFirst(12);
theList.insertFirst(87);
theList.insertFirst(30);
theList.insertLast(22);
theList.insertLast(25);
theList.insertLast(27);

System.out.println(theList);

theList.deleteFirst();
theList.deleteLast();
theList.deleteKey(27);
System.out.println(theList);

theList.insertAfter(12, 10);
theList.insertAfter(30, 21);
System.out.println(theList);
}
}

♥ Reference

[wiki] http://en.wikipedia.org/wiki/Linked_list#Doubly-linked_lists

http://www.google.com.ph/search?hl=en&q=double+linked+list&btnG=Google+Search&meta=

[wiki] http://en.wikipedia.org/wiki/File:Doubly-linked-list.svg

Friday, February 6, 2009

Stacks




1.) Definition and concept of stack

[Wikipedia]

  • In the field of computer science, they said that a stack is an abstract data type and a data structure.
  • LIFO( Last in First out) – principle used by stack

Applications

  • Stacks has a concept which may used as extensively at every level of a modern computer system.
  • PC stacks at the architecture level- which are used in the basic design of an operating system for interrupt handling and operating system function calls. [wiki]

[According to the book]

The fundamental operations on a stack
  • push which is equivalent to an insert, and
  • pop, which deletes the most recently inserted element

[ Mark Allen Weiss,"Data Strustures and Algorithm Analysis in C"

Florida International University, pg 61]

2.) Illustration of Stack


this image is copyright at: [400 x 376 - 182k - jpg - www.sneewittchen.com/blackblackblack/stacks.jpg]


3.) References

http://en.wikipedia.org/wiki/Stack_(data_structure)

[ Mark Allen Weiss,"Data Strustures and Algorithm Analysis in C" Florida International University, pg 61]

www.sneewittchen.com/blackblackblack/

♥♥♥Other Source♥♥♥

http://www.java2s.com/Tutorial/Java/0140__Collections/DoubleEndedListslistwithfirstandlastreferences.htm

http://www.java2s.com/Tutorial/Java/0140__Collections/Adoublylinkedlist.htm

http://www.java2s.com/Tutorial/Java/0140__Collections/Demonstratingastackimplementedasalist.htm

http://www.java2s.com/Tutorial/Java/0140__Collections/AQueueImplementedbyaLinkedList.htm