Argument of varying types? - processing

how do I make a argument of varying types?
I want to do m.add(5) or m.add(float[][]). How would I do that?
void add(? n) {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
data[i][j] += n;
}
}
}
}

You're looking for something called method overloading. You can google that for a ton of results, but basically you'd want to define the function twice:
void add(float n){
// do the thing
}
void add(float[][] n){
// do the thing
}
In theory you could also take an Object parameter and then use the instanceof keyword to figure out what type was actually passed in, but that's a hackier approach.

Related

Sorting algorithm is skipping the last element in my array

I have a simple algorithm to order numbers in an array, all of the elements become ordered except for the last one. I have tried changing the bounds of my loops to fix this, but it just creates an infinite loop instead.
while (pointer < arrayLength){
int min = findMinFrom(pointer);
for (int i = pointer; i < arrayLength; i ++){
if (A[i] == min){
swap(i, pointer);
pointer ++;
}
compNewS ++;
}
}
You see what's the problem? Your pointer will be updated only if A[i] == min if not then it will keep looping. Put your pointer++ out of that condition.
This can be done with only two loops but here is an adjusted version of your code:
public class Numbers {
private static int [] A ;
public static void main(String [] args) {
int [] array = {3,2,1,4,5,6,7,8,9,7};
A = array;
newSort(array, array.length);
for(int i = 0; i < A.length;i++)
System.out.println(A[i]);
}
public static void newSort(int[] array, int arrayLength){
int pointer = 0;
int p = 0;
while(p < array.length) {
int min = findMinFrom(p,array);
int temp = array[p];
array[p] = min;
array[min] = temp;
p++;
}
}
public static int findMinFrom(int p, int[] array){
int min = p;
for (int i = p; i < array.length; i ++){
if (A[i] < array[p]){
min =i;
}
}
return min;
}
}

Can we have an if loop for the root section of Quick Union algorithm?

In the Quick Union algorithm imlementation below, inside the root method can we have an if loop (such as if(i != id[i]) instead of the while loop? I think it works just as well. Then why did they use the while loop?
public class QuickUnionUF {
private int []id;
public QuickUnionUF(int N){
id = new int[N];
for(int i=0; i<N; i++) id[i] = i;
}
private int root(int i){
while(i != id[i]) i = id[i];
return i;
}
public boolean connected(int p, int q){
return root(p) == root(q);
}
public void union(int p, int q){
int i = root(p);
int j = root(q);
id[i] = j;
}
}
A component may be represented by a tree higher than one level. To get the component id you need to go down all the way to the root. For example try
s = new QuickUnionUF(3);
s.union(0,1);
s.union(1,2);
System.out.println(s.connected(0,1)); // <== prints false when using 'if'

Quick Sort (can't Passing the result)

I am absolutely HORRIBLE at passing functions, and even more so with passing arrays.I've problem with my code, I use Quick Sort for sorting my data from External File. The Function can't passing the sort result.
UPDATE : The Function can passing the sort but, sorting in small to big, I want sort big to small , how to do that ?
But if I use simple sorting Like
int temp_score;
string temp_name;
for(int j=0;j<100;j++)
{
for(int k=0;k<100;k++)
{
if(score[j]>score[k])
{
temp_score=score[j];
score[j]=score[k];
score[k]=temp_score;
temp_name=name[j];
name[j]=name[k];
name[k]=temp_name;
}
}
}
No problem with simple sorting.
void quicksort(int score[],string name[],int kiri,int kanan){
int temp_score=0,i=kiri,j=kanan;
int pivot = score[(kiri+kanan)/2];
string temp_name;
//Pengurutan data berdasar Pivot
while (i <= j){
while (score[i]<pivot){
i++;
}while (score[j]>pivot){
j--;
}if (i <= j){
temp_score=score[i];
score[i]=score[j];
score[j]=temp_score;
temp_name=name[i];
name[i]=name[j];
name[j]=temp_name;
i++;
j--;
}
}
//Rekursif
if (kiri<j){
quicksort(score,name,kiri,j);
}if (i<kanan){
quicksort(score,name,i,kanan);
}
}
int main (){
string name[100];
int score[100];
int i=0;
for (int i=0;i<100;i++)
{
name[i]="";
score[i]=0;
}
ifstream highscore;
highscore.open("highscore.txt");
while(!highscore.eof())
{
getline(highscore,name[i]);
highscore >> score[i];
highscore.ignore(100,'\n');
i++;
}
quicksort(score,name,0,99);
cout<<"\tNAMA\t\t|\t\tSCORE\t\t"<<endl;
cout<<"========================================================"<<endl;
for(int m=0;m<25;m++)
{
cout<<name[m]<<"\t\t\t\t"<<"Rp "<<score[m]<<endl;
}
}
To sort in descending order, you would generally replace '<' with '>', i-- with i++, limits are reversed - and I mean as a general guideline, not literally replace blindly everywhere. Ex:
int temp_score;
string temp_name;
for(int j=0;j<100;j++)
{
for(int k=0;k<100;k++)
{
if(score[j]<score[k])
{
temp_score=score[j];
score[j]=score[k];
score[k]=temp_score;
temp_name=name[j];
name[j]=name[k];
name[k]=temp_name;
}
}
}
You can follow this guideline in your other code.
EDIT: Here is a snippet from your code the way you want. Note that I have inserted dummy data instead,
http://ideone.com/KAs1fG
Incorporate this in your code.
How To sort in descending order ?? I so confused
void quicksort(int score[],string name[],int kiri,int kanan){
int temp_score=0,i=kiri,j=kanan;
int pivot = score[(kiri+kanan)/2];
string temp_name;
//Pengurutan data berdasar Pivot
while (i <= j){
while (score[i]<pivot){
i++;
}while (score[j]>pivot){
j--;
}if (i <= j){
temp_score=score[i];
score[i]=score[j];
score[j]=temp_score;
temp_name=name[i];
name[i]=name[j];
name[j]=temp_name;
i++;
j--;
}
}
//Rekursif
if (kiri<j){
quicksort(score,name,kiri,j);
}if (i<kanan){
quicksort(score,name,i,kanan);
}
}

Are these complexity classes correct?

I have a few problems to do and I have a decent understanding of how they work I just want feedback on if I am correct. I need to figure out the big-oh-notation of the following.
1.
public static int[] mystery1(int[] list) {
int[] result = new int[2*list.length];
for (int i=0; i<list.length; i++) {
result[2*i] = list[i] / 2+list[i] % 2;
result[2*i+1] = list[i] / 2;
}
I think this one would be Nlog(N)
2.
public static int[] mystery2(int[] list) {
for (int i=0; i<list.length/2; i++) {
int j = list.length-1-i;
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
return list;
}
I think this one would be O(logN) because it's diving by 2 until it finishes
3.
public static void mystery3(ArrayList<String> list) {
for (int i=0; i<list.size-1; i+=2) {
String first = list.remove(i);
list.add(i+1, first);
}
}
I think this one would be O(N)
4.
public static void mystery4(ArrayList<String> list) {
for (int i=0; i<list.size-1; i+=2) {
String first = list.get(i);
list.set(i, list.get(i+1));
list.set(i+1, first);
}
}
I think this one would be O(N).
All are O(N) except Mystrey3 which is O(N^2)= due to add.list

Algorithm for checking transitivity of relation?

I need to check if relation is transitive or not?
Would you please suggest some algorithm to check the transitivity of relations?
I am storing relation as a boolean matrix there is 1 if elements are related other wise 0 like in graphs.
Thanks.
Much simpler algorithm as my Map/Set version (deleted), now with boolean matrix. Maybe this is easier to understand, even if you don't know Java?
public class Trans
{
final static int SIZE = 4;
static boolean isTransitive(boolean[][] function)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (function[i][j])
{
for (int k = 0; k < SIZE; k++)
{
if (function[j][k] && !function[i][k])
{
return false;
}
}
}
}
}
return true;
}
public static void main(String[] args)
{
boolean[][] function = new boolean[SIZE][SIZE];
for (int i = 0; i < SIZE; i++)
{
function[i] = new boolean[SIZE];
}
function[0][1] = true;
function[1][2] = true;
function[0][2] = true;
function[0][3] = true;
function[1][3] = true;
System.out.println(isTransitive(function));
}
}
Despite this totally sounds like homework...
You'd need to store your relations so that you can look them up by the antecedent very quickly. Then you can discover transitive relations of the type A->B->C, add them to the same storage, and keep going to look up A->B->C->D, etc etc...
Topological sorting may be the right direction. The relationship is transitive if there are no loops in its directed graph representation. If you care about speed, graph algorithms are probably the way to go.

Resources