Ds Codes
Ds Codes
package arrays;
public class arrays {
public static void main(String []args){
int[][] matrix1=new int[3][3];
for(int i=0;i<matrix1.length;i++)
{
for(int j=0;j<matrix1.length;j++)
{
matrix1[i][j]=(int)(Math.random()*100);
System.out.println("the index is: "+i+j+" and value is:
"+matrix1[i][j]);
}
}
//array addition
System.out.println("Array Addition");
int b[][]={{1,2,3},{4,5,6},{7,8,9}};
int c[][]={{1,1,1},{2,2,2},{3,3,3}};
int d[][]= new int[3][3];
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
d[i][j]=b[i][j]+c[i][j];
System.out.print(d[i][j]+" ");
}
System.out.println(" ");
}
//array multiplication
System.out.println("Array Multiplication");
int e[][]={{1,2,3},{4,5,6},{7,8,9}};
int f[][]={{1,1,1},{2,2,2},{3,3,3}};
int g[][]= new int[3][3];
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
g[i][j]=0;
for(int k=0;k<3;k++)
{
g[i][j]+=e[i][k]*f[k][j];
}
System.out.print(g[i][j]+" ");
}
System.out.println(" ");
}
}
}
===================================================================================
===============================================
Occurrence example 1
package occurrance;
import java.util.Scanner;
public class Occurrance {
for(i=0;i<a.length;i++)
{
a[i]=s.nextInt();
}
max=0;
for(int j=0;j<a.length;j++)
{
if(max<a[j])
{
max=a[j];
}
}
x=max;
for(int k=0;k<a.length;k++)
{
if(a[k]==x)
{
count++;
}
}
System.out.println("Largest number is: "+max);
System.out.println("Number of occurrances of element: "+count);
}
===================================================================================
===================================================================================
===================================================================================
========
package grading.on.score;
import java.util.Scanner;
public class GradingOnScore {
===================================================================================
===================================================================================
===================================================================================
========
===================================================================================
===================================================================================
===================================================================================
========
Counting occurrence of each letter example 4
package alphabets;
import java.util.Random;
public class Alphabets {
chars[i]=(char)
S.charAt(generator.nextInt(26));
return chars;
}
System.out.println("STANDARD DAVIATION");
int sum=0;
int max=0;
int min=counts[0];
double sd=0;
for(int i=0;i<counts.length;i++)
{
int average=0;
sd+=((counts[i]-average)*(counts[i]-average))/(counts.length-1);
double standardDeviation=Math.sqrt(sd);
System.out.println("The standard deviation is: "+standardDeviation);
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------------------
package stack;
public Stack(int s)
{
maxsize =s;
stackarray=new long[maxsize];
top=-1;
}
public void push(long j)
{
stackarray[++top]=j;
}
public long pop()
{
return stackarray[top--];
}
public boolean isEmpty()
{
return(top==-1);
}
public static void main(String[] args)
{
Stack s= new Stack(5);
s.push(1);
s.push(2);
s.push(21);
s.push(31);
s.push(34);
System.out.println("VALUES IS");
while(!s.isEmpty())
{
long value =s.pop();
System.out.println(value);
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------
ackage queue1;
Queue1(int size) {
arr = new int[size];
capacity = size;
front = 0;
rear = -1;
count = 0;
-----------------------------------------------------------------------------------
------------------------------------------------
package bubble.sort;
}
if(f== false)
{System.out.println("best case executed");
break;
}
}
// for(int i= 0;i<mainarr.length;i++)
//{
// System.out.println("array:"+mainarr[i]);
//
//}
obj.sort(arr);
for(int i= 0;i<arr.length;i++)
{
System.out.print(" "+arr[i]);
}
}
-----------------------------------------------------------------------------------
------------------------------------------------------
package insertionsorting;
System.out.println("i V H C F");
if(hole == 0)
{
flag = false;
}
System.out.println(i+" "+ value+" "+ hole + " "+ hole + ">"+
z+"&&"+(hole-1)+" "+ flag);
}
False++;
flag = false;
mainarr[hole]=value;
}
System.out.println("-------------------------------");
System.out.println("\nTrue iterations: " + True);
System.out.println("False iterations: " + False);
System.out.println("Total iterations: " + (True+False) );
System.out.println("\n-------------------------------");
System.out.print(arr[j]+" ");
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------
package selectionsort;
public class Selectionsort {
}
}
}
-----------------------------------------------------------------------------------
------------------------------------------------
package quick.sort;
return i+1;
}
System.out.println("sorted array");
printArray(arr);
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------------------
binary treee
class Node
{
int key;
Node left, right;
// Constructors
BinaryTree(int key)
{
root = new Node(key);
}
BinaryTree()
{
root = null;
}
/*create root*/
tree.root = new Node(1);
1
/ \
null null */