Posts

Showing posts with the label Data Structures

Merging Two Sorted Arrays in Sorted Order

Today with heavy rainy. I fully wet. and come to the lab and start to code. What I did.  Sorting two arrays in independently and I merge them in sorted order. How am I do. see below...  Your reiew is our success. Think how we modify this in more.   

Insertion Sort

Image
Insertion Sort is a simple sorting algorithm that build final sorted element in the array. The insertion sort give more efficient way and give more advantage than other sort like quick sort, merge sort and heap sort.  It's specialties are : Simple More efficient ( Time Complexity O(n 2 ) ) Only required constant amount of memory requirement. ( O(1) ) Algorithm ------------------------------------------------------------------------------------------------------------ Insertion_Sort(array){      for i=1 to n-1{           element = array[i];           j=i;           while(j>0 and array[j-1] > element ){                  array[j] = array[j-1];               ...

Data Structures : Stack

Image
Stack is the data structure that can be logically through as Linear Data Structure. The basic implementation is called as LIFO ( Last In First Out ) way.  

Merge Sort

Image

Game Entry

*************************************************************************** public class GameEntry{ public String name; public int score; public GameEntry(String n, int s){ name = n; score = s; } public String getName(){ return name; } public int getScore(){ return score; } public String toString(){ return "("+name+","+score+")"; } } ************************************************************************** import java.util.Scanner; public class Scores{ public static final int maxEntries = 10; public GameEntry[] entries; public int numEntries; public Scores(){ entries = new GameEntry[maxEntries]; numEntries =0; } public void setEntry(String name, int score){ GameEntry gE  = new GameEntry(name,score); if(numEntries ==0){ entries[numEntries] = gE; numEntries++; } else if(numEntries >0 && numEntries !=0 && numEntries<=10){ entries[numEntries]...

Hole with Random Generator

import java.util.Scanner; public class randomHole{ String[][] holeStore; static int Ncol=5; static int Nrow=3; int token; randomHole(){ holeStore = new String[Nrow][Ncol]; for(int i=0; i<Nrow; i++){ for(int j=0; j<Ncol; j++){ holeStore[i][j] = "Free"; } } } void set_full(int row,int col){ findToken(); boolean valid_token = false; for(int i=0; i<Nrow; i++){ for(int j=0; j<Ncol; j++){ if(holeStore[i][j] == Integer.toString(token)){ set_full(row,col); } else{ valid_token = true; } } } if(valid_token){ holeStore[row-1][col-1] = Integer.toString(token); System.out.println("Your Token is : "+token); } } void set_Null(int row, int col){ holeStore[row-1][col-1] = "Free"; } void findHole(int token){ boolean is_found = false; for(int i=0; i<Nrow; i++){ for(int j=0; j<Ncol; j++){ if( holeStore[i][j].equals(Integer...

Hole without random

import java.util.Scanner; public class hole2{ String[][] holeStore; int Ncol=5; int Nrow=3; int token; hole2(){ holeStore = new String[Nrow][Ncol]; for(int i=0; i<Nrow; i++){ for(int j=0; j<Ncol; j++){ holeStore[i][j] = "Free"; } } } void set_full(int row,int col){ findToken(row,col); holeStore[row-1][col-1] = Integer.toString(token); } void set_Null(int row, int col){ holeStore[row-1][col-1] = "Free"; } void findHole(int token){ boolean is_found = false; for(int i=0; i<Nrow; i++){ for(int j=0; j<Ncol; j++){ if( holeStore[i][j].equals(Integer.toString(token))){ int row = i+1; int col = j+1; System.out.println("Your Row is : "+row); System.out.println("Your Colum is "+col); is_found = true;      break; } } } if(is_found == false){ System.out.println("Sorry Your Token Not Found!\nTry Again."); } }...

Selection Sort

Image
  these picture shows that the algorithm of the selection sort the code is below in (java) ****************************************************************** public class selectionSort{ public void sort(int[] A){ for(int i=0; i<A.length; i++){ int minIndex =i; for(int j=i; j<A.length; j++){ if(A[minIndex] > A[j]){ minIndex = j; } }    // Swapping the minIndex Element and first element  int temp = A[i]; A[i] = A[ minIndex ]; A[ minIndex ] = temp; } } } ****************************************************************** public class selectionSortDemo{ public static void main(String args[]){ int A[] = {7,3,2,5,1}; selectionSort S = new selectionSort(); S.sort(A); for(int i=0; i<A.length; i++){ System.out.print(A[i]+" "); } } } ******************************************************************************

Bubble Sort in Java

Image
Bubble Sort is a simple sorting algorithm. It repeatedly comparing each of the adjacent pair and swap them like the above animation.