So I have done a Bucket Sort algorithm that works just fine, but now I'm being asked to do it recursively. The Bucket Sort itself is something along the lines of:
public static void bucketSort(int[] arr, int max) {
int[] bucket = new int[max + 1];
for (int i = 0; i < arr.length; i++) {
bucket[arr[i]]++;
}
int x = 0;
for (int i = 0; i < bucket.length; i++) {
for (int j = 0; j < bucket[i]; j++) {
arr[x++] = i;
}
}
}
So my question would be, how to make this same algorithm but recursively?
Note: I'm coding in Java using IntelliJ, but feel free to explain in other languages if you prefer it.
Thanks in advance!
Related
I am trying to scan an one-dimensional array for Singular Value Decomposition(SVD) and the worst time and space complexity to be O(n) without using any secondary data structure.
They only way I manage to do it is with a nested loop, but that is making it O(n^2)
public static void svd(Integer[] array){
int count = 0;
int svd = 0;
for (int i = 0; i < array.length; i++) {
count=0;
for (int j = 0; j < array.length; j++) {
if(array[i] == array[j]){
count++;
}
if(count>(array.length/2)){
svd = array[i];
System.out.println("svd = "+svd);
}
else if(count<array.length/2){}
}
}
}
The awnser is using the Boyer-Moore majority vote algorithm
https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
I am really new to java and having a bit of trouble with this. I have looked at other code on here and other places with similar problems but I do not understand the library files, etc. I'm trying to understand the basics now. Any help would be appreciated. My current code is:
public static void main(String[] args) {
double[][] father = new double[25][25];
for (int i = 0; i < 25; i++){
father[i] = Math.random();
for (int j = 0; j < 25; j++){
father[j] = Math.random();
}
}
I dont know java but a 2d array should work like this
public static void main(String[] args) {
double[][] father = new double[25][25];
for (int i = 0; i < 25; i++){
for (int j = 0; j < 25; j++){
father[i][j] = Math.random();
}
}
You are trying to set an array of doubles to a double. when trying to specify a certain item in the 2d array, always use arrayName[index1][index2].
public static void main(String[] args) {
double[][] father = new double[25][25];
for (int i = 0; i < 25; i++){
for (int j = 0; j < 25; j++){
father[i][j] = Math.random();
}
}
A two-dimensional array, such as you have, requires both indexes in order to refer to a specific element. For example father[3][6] is an element of the array (a double, because that's the type of the array), but father[i] is not.
Also, you should use the array lengths, not hard-coded values, as the iteration limits. That way, if the array's size changes, you don't need to change the limits as well. Instead of for (int i = 0; i < 25; i++) you should use for (int i = 0; i < father.Length; i++) so that if the length of the array changes you still iterate over the whole thing without overflowing.
All in all:
double[][] father = new double[25][25];
for (int i = 0; i < father.Length; i++) {
for (int j = 0; j < father[i].Length; j++) {
father[i][j] = Math.random();
}
}
i have an array of integers like this one :
A={1,1,4,4,4,1,1}
i want to count the each number once , for this example the awnser is 2 becuase i want to count 1 once and 4 once
i dont want to use sorting methods
i am unable to find a way to solve it using java.
i did this but it gives me 0
public static void main(String args[]) {
int a[] = { 1,1,4,4,4,4,1,1};
System.out.print(new Test4().uniques(a));
}
public int uniques(int[] a) {
int unique = 0;
int tempcount = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i] == a[j]) {
tempcount++;
}
}
if (tempcount <= 2) {
unique=a[i];
}
tempcount = 0;
}
return unique;
}
the purpose of the question is to understand the logic of it but not solving it using ready methods or classes
This one should work. I guess this might be not the most elegant way, but it is pretty straightforward and uses only simple arrays. Method returns number of digits from array, but without counting duplicates - and this I believe is your goal.
public int uniques(int[] a) {
int tempArray[] = new int[a.length];
boolean duplicate = false;
int index = 0;
int digitsAdded = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < tempArray.length; j++) {
if (a[i] == tempArray[j]) {
duplicate = true;
}
}
if(!duplicate) {
tempArray[index] = a[i];
index++;
digitsAdded++;
}
duplicate = false;
}
//this loop is needed if you have '0' in your input array - when creating temp
//array it is filled with 0s and then any 0 in input is treated as a duplicate
//again - not most elegant solution, maybe I will find better later...
for(int i = 0; i < a.length; i++) {
if(a[i] == 0) {
digitsAdded++;
break;
}
}
return digitsAdded;
}
Okay first of all in your solution you are returning the int unique, that you are setting as the value that is unique a[i]. So it would only return 1 or 4 in your example.
Next, about an actual solution. You need to check if you have already seen that number. What you need to check is that for every number in the array is only appears in front of your position and not before. You can do this using this code below.
public int uniques(int[] a) {
int unique = 1;
boolean seen = false;
for (int i = 1; i < a.length; i++) {
for (int j = 0; j < i; j++) {
if (a[i] == a[j]) {
seen = true;
}
}
if (!seen) {
unique++;
}
seen = false;
}
return unique;
}
In this code you are iterating over the number you have seen and comparing to the number you are checking (a[i]). You know that for it to be unique you cant have seen it before.
I see two possible solutions:
using set
public int unique(int[] a) {
Set<Integer> set = new HashSet<>();
for (int i : a) {
set.add(i);
}
return set.size();
}
using quick sort
public int unique(int[] a) {
Arrays.sort(a);
int cnt = 1;
int example = a[0];
for (int i = 1; i < a.length; i++) {
if (example != a[i]) {
cnt++;
example = a[i];
}
}
return cnt;
}
My performance tests say that second solution is faster ~ 30%.
if restricted to only arrays, consider trying this:
Lets Take a temporary array of the same size of orignal array, where we store each unique letter and suppose a is your orignal array,
int[] tempArray= new int[a.length];
int tempArraycounter = 0;
bool isUnique = true;
for (int i = 0; i < a.length; i++)
{
isUnique = true;
for (int j = 0; j < tempArray.length; j++)
{
if(tempArray[j] == a[i])
isUnique = false;
}
if(isUnique)
{
tempArray[tempArraycounter] = a[i];
tempArraycounter++;
isUnique = false;
}
}
now tempArraycounter will be your answer ;)
Try Following code:
int test[]={1,1,4,4,4,1,1};
Set<Integer> set=new LinkedHashSet<Integer>();
for(int i=0;i<test.length;i++){
set.add(test[i]);
}
System.out.println(set);
Output :
[1, 4]
At the end set would contain unique integers.
I cannot seem to make sense of this particular algorithm. It seems to be a bubblesort but not in a traditional sense. What is it?
public static void main(String[] args)
{
double[] a = {0.75, 0.5, 1.0};
sort(a);
for (int i = 0; i < a.length; i++)
System.out.println(a[i]);
}
public static void sort(double[] tal)
{
double p = 0;
int k = 0;
for (int i = 0; i < tal.length - 1; i++)
{
k = i;
for (int j = i + 1; j < tal.length; j++)
{
if (tal[j] < tal[k])
k = j;
}
p = tal[i];
tal[i] = tal[k];
tal[k] = p;
}
}
At first, I incorrectly thought it an Insertion sort. It is a Selection sort (from the Wikipedia entry)
It is a selection sort. The inner loop finds the smallest among the remaining elements which is then exchanged with the first element of the unsorted range.
It's selection sort. Insertion sort works with a forand a while loop.
I have a homework assignment to sort an array in ascending order. Obviously, this is to be done manually without using any kind of sort() function.
I figured to do it, I would need two for loops: the first one will loop through the existing array and create a temporary value with the value and index of the array. The second loop will compare the temporary values to the existing values and sort them. I keep trying to write the code, but I just can’t seem to get it right. Here is the latest method I came up with:
public int[] sortArray (int[] inArray)
{
//Construct the array we're using here
int[] newArray = inArray;
for(int x = 0; x < a.length; x++) //a.length = # of indices in the array
{
int tempValue = a[x];
int tempIndex = x;
for(int y = 0; y < a.length; y++)
{
if(tempValue < a[y])
{
newArray[x] = tempValue;
}
}
}
return newArray;
}
I’m pretty sure this is incorrect, but if someone could push me in the right direction it would be very appreciated!
You have a nearly OK version of the Selection Sorter. You need to start your y at x+1, not at 0. Otherwise you're re-scanning the sorted portion of the array. You should also note that selection sort is an in-place algorithm; if you are looking to make a copy of the array, you should use Arrays.copy method, otherwise int[] newArray = inArray;
is creating an alias, not a copy. Finally, the if statement in the nested loop should swap a[x] and a[y], not simply put tempValue in:
if(newArray[x] < newArray [y]) {
int tempValue = newArray[y];
newArray[y] = newArray[x];
newArray[x] = tempValue;
}
Instead of trying to invent your own sorting algorithm, I'd urge you to study what already exists. There's a ton of prior art on this.
Take a look at the Wikipedia article: Sorting algorithm.
Bubble sort is very easy to implement, but has quadratic complexity (the same as your current attempt).
Quicksort isn't too hard to implement either, and has better average complexity.
int minval = input[0];
int temp=0;
for(int i = 0; i< input.length; i++)
{
for(int j = 0; j< input.length-1; j++)
{
if(input[j+1]<input[j])
{
temp=input[j+1];
input[j+1]=input[j];
input[j]=temp;
}
}
}
The sorting you are trying to achieve is called Bubble sort - the wikipedia entry is pretty good you should read it. Though, it's never really used because there is better alternative - Insertion sort (An example is Timsort in Python which is a hybrid of Merge sort and Insertion sort). These two are the basic algorithms that fits your idea with two loops, hence O(n2) complexity.
You should also consider different algorithms for your assignment or, at least, be aware of:
Merge sort
Quicksort
Hope it helps.
int arr[] = new int[]{10, 20, 5, 6, 30, 1, 2};
boolean bool = true;
int t = 0;
while (bool) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
int c = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = c;
t++;
}
}
if (t == 0) {
bool = false;
}
t = 0;
}
for (int y : arr) {
System.out.println(y);
}
int[] number = { 1,2,1,3,5,4 };
int temp;
for (int i = 0; i < number.length; i++)
{
for (int j = i + 1; j < number.length; j++)
{
if (number[i] > number[j])
{
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
}
for (int i = 0; i <number.length; ++i)
System.out.println(number[i]);
}