Tuesday, August 13, 2013

Java Linear and Binary Searching - For my reference


public class Searching {

/**
* @param args
*/
public static void main(String[] args) {

int arrayNumbers[] = new int[] {55, 65, 76, 89, 123, 43, 56, 23, 11, 107, 98 };
int searchNumber = 89;
int pos = 0;
int posB = 0;
int unsortedArray[] = new int[arrayNumbers.length];
System.arraycopy( arrayNumbers, 0, unsortedArray, 0, arrayNumbers.length);
arrayNumbers = sort(arrayNumbers);

for (int i = 0; i < unsortedArray.length;i++){


pos = linearSearch(arrayNumbers, unsortedArray[i]);
posB = binarysearch(arrayNumbers, unsortedArray[i]);
System.out.println(unsortedArray[i] + "\t" + pos + "\t" + posB + "\t" + arrayNumbers[i]);
}
}

private static int binarysearch(int[] arrayNumbers, int i) {

int lower = 0;
int upper = arrayNumbers.length - 1;
int cur = 0;

while (true){

cur = (lower + upper) / 2;

if(i == arrayNumbers[cur])

return cur;
else if (lower > upper) 

return arrayNumbers.length;

else {

if(i > arrayNumbers[cur])

lower = cur + 1;

else 

upper = cur - 1;
}
}


}

private static int linearSearch(int[] arrayNumbers, int searchNumber) {

for(int i = 0; i < arrayNumbers.length; i++){

if(searchNumber == arrayNumbers[i]){

return i;
}

}
return 0;
}

public static int[] sort (int[] arrayNumbers){

int temp = 0;
for (int i = 0; i < arrayNumbers.length ; i ++ ){

for (int j = 0; j < arrayNumbers.length; j++) {

if(arrayNumbers[i] < arrayNumbers[j]){

temp = arrayNumbers[i];
arrayNumbers[i] = arrayNumbers[j];
arrayNumbers[j] = temp;
}
}
}
return arrayNumbers;
}
}


Friday, August 9, 2013

linked list - simple adding and deleting in between - for my reference

#include "stdio.h"
#include "stdlib.h"

struct node {
   
   int   data;
   struct node* next;
};

int length(struct node *head){
    
    struct node* current = head;
    int count = 0;
    while ( current != NULL ){
          
          count++;
          current = current -> next;
    }
return count;
}

void display(struct node *head) {
     
      
    struct node* current = head;
    int count = 0;
    while ( current != NULL ){
          
          printf("%d \t", current -> data);
          current = current -> next;
    } 
    printf("\n \n");
}

void delete ( struct node* head, int pos) {
     
     int k = 0;
     struct node* current = head;
     for( k = 0; k < pos; k++){
          
          current = current -> next;
     }
     current -> next = current -> next -> next;
}

void add ( struct node* head, int pos, int data) {
     
     int k = 0;
     struct node* current = head;
     struct node* newNode = malloc(sizeof(struct node));
     for( k = 0; k < pos; k++){
          
         current = current -> next;
     }
     
     newNode -> next = current -> next;
     newNode -> data = data;
     current -> next = newNode;
}         
     
int main(){

    int k = 0;
    int m = 0;
    int i =0;
    int pos = 3;
    struct node* head = NULL;
    struct node* current = NULL;
    struct node* nextCur = NULL;
    
    head = malloc (sizeof (struct node));
    current = malloc (sizeof (struct node));
    head -> data = 1;
    head -> next = current;
    current -> next = NULL;
    
    for(i = 0; i < 5; i++){  
          
         current -> data = i;
         nextCur = malloc (sizeof (struct node));
         current -> next = nextCur;
         current = nextCur;
     }
   current -> data = i; 
   current -> next = NULL; 
      
   display(head);    
   
   delete (head, pos);
   
   display(head);   
   
   add (head, pos, 10); 
   
   display(head); 
    
getch();
return 0;
}
    

Simple Linked List -for my reference !!


#include "stdio.h"
#include "stdlib.h"


struct node {
   
   int   data;
   struct node* next;
};

int length(struct node *head){
    
    struct node* current = head;
    int count = 0;
    while ( current != NULL ){
          
          count++;
          current = current -> next;
    }
return count;
}

void display(struct node *head) {
     
      
    struct node* current = head;
    int count = 0;
    while ( current != NULL ){
          
          printf("%d \t", current -> data);
          current = current -> next;
    } 
    printf("\n \n");
}
     
int main(){

    int k = 0;
    struct node* head = NULL;
    struct node* second = NULL;
    struct node* third = NULL;
    struct node* twothree = NULL;
    
    head = malloc (sizeof (struct node));
    second = malloc (sizeof (struct node));
    third = malloc (sizeof (struct node));
    twothree = malloc (sizeof(struct node));
  
    head -> data = 1;
    head -> next = second ;
    
    second -> data = 2;
    second -> next = third;
    
    third -> data = 3;
    third -> next = NULL;
    
    printf("List with three elements \n");
   
    display(head);
    k = length(head);
    printf("\n adding a node in between second and third \n");
    
    twothree -> data = 10;
    second -> next = twothree;
    twothree -> next = third;
    
    display(head);
    k = length(head);
    display(head);
    
    printf ("\nThe length of the list %d\n \n", k);
    
    printf ("\nDeleting a node twothree \n \n");
    
    second -> next = third;
    
    k = length(head);
    display(head);
    printf ("The length of the list %d \n \n", k);
    
    
getch();
return 0;
}