Selection sort: storing value instead of index - java

I'm studying sorting algorithms, including selection sort, so i decided to write a method and it works fine, but when i checked the book it had 2 variables so i checked it and found that it's using a variable to store the current index and the other as temporary to swap
while mine had only the temporary variable that also stored the initial value in the index as the lowest, then compared it to the other values in the array and swapped if a larger value was found.
Here's my code:
public static void selectionSort(int[] arr){
int lowest;
for(int i = 0; i < arr.length - 1; i++){
lowest = arr[i];
for(int j = i+1; j<arr.length; j++){
if(arr[j]<lowest){
lowest = arr[j];
arr[j] = arr[i];
arr[i] = lowest;
}
}
}
}
and Here's the book's
public static void selectionSort(int[] list){
int min;
int temp;
for(int i = 0; i < list.length - 1; i++) {
min = i;
for(int j = i + 1; j < list.length; j++)
if( list[j] < list[min] )
min = j;
temp = list[i];
list[i] = list[min];
list[min] = temp;
}
}
so I looked on the web and all of them follow the same way as the book, so is my code bad or slower, or it is not considered Selection sort ?
sorry for any english mistakes :P

So, the original code works like this:
Start with the first element of the array
Find the smallest number in the array
Swap the two numbers
Repeat the process Until You reach the end of the list
While Yours does this:
Start with the first element of the array
For each element smaller than the current Swap the two numbers
Replace the lowest number with the swapped
Repeat the process
The result should be the same, but probably You are swapping more numbers than the first one. This probably will make it a little bit slower than the original.
Actually it kinda looks like the insertion sort now, so basically You are swapping the elements with all that are bigger than the one You have.

It looks like you're doing a lot more swaps in the nested for loop.
What happens if you do a sort of [4,3,2,1]? I think you'll have more swap operations than the actual selectionSort.
I'm not sure if your code is bad or slower.
SelectionSort is known to be correct, but it isn't fast either.

Related

Mergesort & recursion confusion/code not working

I've been at this for a couple days, reading many pseudocode and watching videos to explain recursion and mergesort. I understand mergesort and somewhat understand recursion -- except for when it applies to arrays as is in my code below. I did some debugging and it appears that my program is not sorting correctly regardless of the out of bounds error. I am very lost and would appreciate any help you can offer!
Questions:
1) what does it mean for a recursion on an array? Does it create a sub array that is held by the original array? -- if that makes sense.
2) why is my code running into a out of bounds error even though I followed a tutorial to the t and also set the k value after every pass. Specifically the issue is being encountered.
Here's the code:
public class Merge {
public static void main(String[] args) {
}
static void mergeSort(int arr[]){
int r = arr.length - 1;
Merge.sort(arr,0,r);
System.out.println(arr);
}
static void sort(int arr[], int p, int r){
if(p<r){
int q = (p+r)/2;
sort(arr,p,q);
sort(arr,q+1,r);
merge(arr,p,q,r);
}
}
static void merge(int arr[], int p, int q, int r){
int n1 = q-p+1;
int n2 = r-q;
int L[] = new int[n1];
int R[] = new int[n2];
for(int i = 0; i< n1; i++){
L[i] = arr[i];
}
for(int j = 0; j< n2; j++){
R[j] = arr[q+1+j];
}
int i = 0, j = 0;
int k = 1;
while(i<n1 && j<n2){
if(L[i]<= R[j]){
arr[k] = L[i];
i++;
}
else{
arr[k] = R[j];
j++;
}
k++;
}
while(i<n1){
arr[k] = L[i];
i++;
k++;
}
Error occurs here --> while(j<n2){
arr[k] = R[j];
k++;
}
}
}
Thank you for the help!
edit: Just wanted to say how greatful I am for the amazing replies on this post, thank you so much for your time.
To be honest I don't think your sentence 'recursion on an array' makes any sense.
Your code has one array arr which gets sorted. Your merge method is supposed to be sorting parts of this array, but every time it is called it has the same whole array object. There are no sub-arrays; it's just up to this method to sort the relevant part of this one array. If this method isn't doing what it's supposed to do, then problems will occur.
Let's take a closer look at the loop where you are getting an error:
while(j<n2){
arr[k] = R[j];
k++;
}
Suppose we get to this loop with j < n2. What happens?
We enter the loop because j < n2, so we copy R[j] to arr[k] and then increment k. We go back to the top of the loop, we find j is still less than n2 because neither variable has changed, so we copy R[j] to arr[k] and increment k again. We got back to the top of the loop, find j is still less than n2 and go round again. And so on, and so on, until eventually k falls off the end of arr and we get an ArrayIndexOutOfBoundsException.
In this part of mergesort we are trying to copy into arr the contents of R that haven't already been merged into arr, but we forgot to increment j. So, to fix this loop, increment j as well as k:
while(j<n2){
arr[k] = R[j];
j++;
k++;
}
Note that the previous loop, the one beginning with while(i<n1), increments i and k. This change now makes the two loops look more similar to one another.
So, we run our code again, and what happens? We still get an ArrayIndexOutOfBoundsException. Clearly we haven't solved the problem yet, but have we made any progress at all if we're just getting the same error?
The intention of the merge method is to merge the subarrays of arr from positions p to q inclusive and from positions q+1 to r inclusive. If the two subarrays are sorted, then after merging the whole subarray of arr from p to r will be sorted.
However, when we write the values back into arr, we start at index 1. Is this correct? Suppose arr has length 2, p = 0, q = 0 and r = 1. We have two elements to sort. Where does the first one get written to, and where does the second?
The answer is the first one gets written to arr[1], and your code throws an exception because it attempts to write the second to arr[2], which does not exist.
You want k to start from the start of the subarray you are sorting. You want k to start from p.
So replace the line
int k = 1;
with
int k = p;
We try again, and now we find the code no longer throws an exception but prints something unintelligible like [I#65fb1cd. Annoyingly, this is how Java prints arrays by default. To fix this, add the line import java.util.Arrays; to your file and replace the line
System.out.println(arr);
with
System.out.println(Arrays.toString(arr));
Your code should now print out a list of numbers when it runs.
However, we now see that our code isn't sorting the array correctly. I asked it to sort the values 8, 1, 4, 9 and it came back with 1, 1, 8, 9. The 1 has been duplicated and the 4 has disappeared.
Recall once again that the intention of the merge method is to sort arr from p to r onwards. Take a careful look at what values are being copied from the array into L and R:
for(int i = 0; i< n1; i++){
L[i] = arr[i];
}
for(int j = 0; j< n2; j++){
R[j] = arr[q+1+j];
}
Notice any difference between these two loops, apart from the fact that one uses j instead of i, n2 instead of n1 and R instead of L?
Note that when you copy into R, you are copying values from position q+1 onwards. These are the values in the second sorted subarray. But when you are copying into L, you are copying values from position 0 onwards. This isn't necessarily where the first sorted subarray begins. That of course starts from p.
Replace the first of these loops with:
for(int i = 0; i< n1; i++){
L[i] = arr[p+i];
}
Finally, we run the code and find that we now have a working mergesort program.
Let's break your question down a bit - specifically, what does recursion mean? You can think of it like a loop - it performs an operation on itself until it reaches a stop condition. Take for example, a for loop
for(int i = 0; i < 2; i++)
will perform the operation until it reaches the case where variable i is no longer less than 2. Likewise, recursively
void methodLoop(int input){
int i = input;
if(i < 2){
methodLoop(i+1);
}
else{
System.out.println("Base case reached! I is no longer less than 2!");
}
}
Performs a similar operation, just with recursion instead!
What does this mean for arrays? It depends. What you've touched upon in your question is a concept called multidimentional arrays - arrays within arrays. These work like normal arrays, it's just an array that contains another array in each one of its indexes - these are instantiated as follows
String[][] multidimensionalarray = new array[4][4]
To visualize such a concept, it might be easier to think of it as a coordinate grid, with the indexes being the coordinate places and the value at that index containing information about that place. For example, assuming the multidimensional array has been filled with data like so, it might look like:
4 a b c d
3 e f g h
2 i j k l
1 m n o p
1 2 3 4
and then the value of multidimensionarray[2][3] would return the string k!

Descending Selection Sort of Object Array in Java

I'm currently working on a Bank Account program that takes user input and enters it into an array. It performs actions like deposit, withdraw, search, etc. I'm currently stuck utilizing a selection sort based on the balance amounts in each account. The program should sort the accounts based on balance from highest to lowest and print the results to the screen.
This is my first time using selection sort and one of my first few times using arrays. I understand the concept of selection sort and how to do it with primitive values, but translating that to object values is stumping me. Below is the code I have for the selection sort.
if (out.equals("Sort")) {
int i, j, maxIndex;
double maxValue;
//getNumberOfAccounts is a static counter incremented each time
//a new bank account is created
for (i = 0; i < BankAccount.getNumberOfAccounts(); i++) {
//Sets first value as largest
maxValue = BankAccounts[i].getBalance();
maxIndex = i; //Index of first value
for (j = i; j == BankAccount.getNumberOfAccounts(); j++) {
//Compares subsequent values to initial max value
if (BankAccounts[j].getBalance() > maxValue) {
maxValue = BankAccounts[j].getBalance();
maxIndex = j;
}
}
//Attempts to swap values
BankAccount temp = BankAccounts[i];
BankAccounts[i] = BankAccounts[maxIndex];
BankAccounts[maxIndex] = temp;
//Outputs Bank Account data in descending order based on balance
BankAccounts[maxIndex].printReport();
}
}
Notes:
-This is a portion of the full program so if I'm missing a bracket it's just because I didn't copy the whole thing.
-It seems that when I run the program it does not store the maxValue; the output of maxValue instead outputs whichever value of the loop iteration is next.
-When I run the program it simply prints the bank accounts in the exact order I enter them.
Thank you in advance and if there's any more information I can provide I gladly will.
When you're doing BankAccounts[maxIndex].printReport();, you've already swap the values. So you're printing the value in position maxIndex, and there is already item that was at position i at the beginning of current step.
So, you need to do BankAccounts[maxIndex].printReport(); before swap, or print the value from the right position — BankAccounts[i].printReport();
About maxValue — it's updates each step, so if you need this after cycle ends, you can just get it as BankAccounts[0].getBalance() after sort routine ends.
Moreover, if you need only to sort items, but you're not tied to use selection sort, I'd like to recommend Java built-in methods for sort, so your code should look like this:
Arrays.sort(BankAccounts, 0, BankAccount.getNumberOfAccounts(), new Comparator<BankAccount>() {
#Override
public int compare(BankAccount o1, BankAccount o2) {
if (o1.getBalance() > o2.getBalance()) return -1;
if (o1.getBalance() < o2.getBalance()) return 1;
return 0;
}
}
);
After this sort operation, array BankAccounts is sorted in descending order of balances, and you can print reports just in a simple loop:
for (i = 0; i < BankAccount.getNumberOfAccounts(); i++) {
BankAccounts[i].printReport;
}
First you need to change this line
for (j = i; j == BankAccount.getNumberOfAccounts(); j++) {
to
for (j = i; j < BankAccount.getNumberOfAccounts(); j++) {
The way you have it now you're saying "loop until j is equal to BankAccount.getNumberOfAccounts()" and that’s never gonna happen in this code.
Another performance issue that you have.
BankAccount temp = BankAccounts[i];
BankAccounts[i] = BankAccounts[maxIndex];
BankAccounts[maxIndex] = temp;
Imagen that the current index is already in the right spot. You will make an unnecessary "switch".
For example: current intex ( i == 5 ) biggest balance position ( maxIndex == 5 ).
You will have:
BankAccount temp = BankAccounts[5];
BankAccounts[5] = BankAccounts[5];
BankAccounts[5] = temp;
So you can change this part to this:
if(i != maxIndex) {
//Attempts to swap values
BankAccount temp = BankAccounts[i];
BankAccounts[i] = BankAccounts[maxIndex];
BankAccounts[maxIndex] = temp;
}
After following a couple of old related threads I stumbled upon a thread that perfectly answered my question. While the Array.sort method mentioned above worked perfectly, I was uncomfortable using it simply because I have not yet learned about it.
if (out.equals("Sort")){
for (int i = 0; i < BankAccount.getNumberOfAccounts(); i++){
for(int j = i+1; j < BankAccount.getNumberOfAccounts(); j++){
if(BankAccounts[j].getBalance() > BankAccounts[i].getBalance()){
BankAccount [] temp = new BankAccount [BankAccounts.length];
temp [j] = BankAccounts [j];
BankAccounts [j] = BankAccounts [i];
BankAccounts [i] = temp [j];
}
}
}
for (int i = 0; i < BankAccount.getNumberOfAccounts(); i++){
BankAccounts[i].printReport();
System.out.println();
}
Here's the code that printed the results I've been looking for. I realized my comparison in the inner loop was off and I wasn't creating a new array for the swap, only new objects. Thanks for all the help!

No. of ways to divide an array

I want to find The number of ways to divide an array into 3 contiguous parts such that the sum of the three parts is equal
-10^9 <= A[i] <= 10^9
My approach:
Taking Input and Checking for Base Case:
for(int i=0;i<n;i++){
a[i]= in.nextLong();
sum+=a[i];
}
if(sum%3!=0)System.out.println("0");
If The answer is not above Then Forming the Prefix and Suffix Sum.
for(int i=1;i<=n-2;i++){
xx+=a[i-1];
if(xx==sum/3){
dp[i]=1;
}
}
Suffix Sum and Updating the Binary Index Tree:
for(int i=n ;i>=3;i--){
xx+=a[i-1];
if(xx==sum/3){
update(i, 1, suffix);
}
}
And Now simple Looping the array to find the Total Ways:
int ans=0;
for(int i=1;i<=n-2;i++){
if(dp[i]==1)
{
ans+= (query(n, suffix) - query(i+1, suffix));
// Checking For the Sum/3 in array where index>i+1
}
}
I Getting the wrong answer for the above approachI don't Know where I have made mistake Please Help to Correct my mistake.
Update and Query Function:
public static void update(int i , int value , int[] arr){
while(i<arr.length){
arr[i]+=value;
i+=i&-i;
}
}
public static int query(int i ,int[] arr){
int ans=0;
while(i>0){
ans+=arr[i];
i-=i&-i;
}
return ans;
}
As far as your approach is concerned its correct. But there are some points because of which it might give WA
Its very likely that sum overflows int as each element can magnitude of 10^9, so use long long .
Make sure that suffix and dp array are initialized to 0.
Having said that using a BIT tree here is an overkill , because it can be done in O(n) compared to your O(nlogn) solution ( but does not matter if incase you are submitting on a online judge ).
For the O(n) approach just take your suffix[] array.And as you have done mark suffix[i]=1 if sum from i to n is sum/3, traversing the array backwards this can be done in O(n).
Then just traverse again from backwards doing suffix[i]+=suffix[i-1]( apart from base case i=n).So now suffix[i] stores number of indexs i<=j<=n such that sum from index j to n is sum/3, which is what you are trying to achieve using BIT.
So what I suggest either write a bruteforce or this simple O(n) and check your code against it,
because as far as your approach is concerned it is correct, and debugging is something not suited for
stackoverflow.
First, we calculate an array dp, with dp[i] = sum from 0 to i, this can be done in O(n)
long[]dp = new long[n];
for(int i = 0; i < n; i++)
dp[i] = a[i];
if(i > 0)
dp[i] += dp[i - 1];
Second, let say the total sum of array is x, so we need to find at which position, we have dp[i] == x/3;
For each i position which have dp[i] == 2*x/3, we need to add to final result, the number of index j < i, which dp[j] == x/3.
int count = 0;
int result = 0;
for(int i = 0; i < n - 1; i++){
if(dp[i] == x/3)
count++;
else if(dp[i] == x*2/3)
result += count;
}
The answer is in result.
What wrong with your approach is,
if(dp[i]==1)
{
ans+= (query(n, suffix) - query(i+1, suffix));
// Checking For the Sum/3 in array where index>i+1
}
This is wrong, it should be
(query(n, suffix) - query(i, suffix));
Because, we only need to remove those from 1 to i, not 1 to i + 1.
Not only that, this part:
for(int i=1;i<=n-2;i++){
//....
}
Should be i <= n - 1;
Similarly, this part, for(int i=n ;i>=3;i--), should be i >= 1
And first part:
for(int i=0;i<n;i++){
a[i]= in.nextLong();
sum+=a[i];
}
Should be
for(int i=1;i<=n;i++){
a[i]= in.nextLong();
sum+=a[i];
}
A lot of small errors in your code, which you need to put in a lot of effort to debugging first, jumping to ask here is not a good idea.
In the question asked we need to find three contiguous parts in an array whose sum is the same.
I will mention the steps along with the code snippet that will solve the problem for you.
Get the sum of the array by doing a linear scan O(n) and compute sum/3.
Start scanning the given array from the end. At each index we need to store the number of ways we can get a sum equal to (sum/3) i.e. if end[i] is 3, then there are 3 subsets in the array starting from index i till n(array range) where sum is sum/3.
Third and final step is to start scanning from the start and find the index where sum is sum/3. On finding the index add to the solution variable(initiated to zero), end[i+2].
The thing here we are doing is, start traversing the array from start till len(array)-3. On finding the sum, sum/3, on let say index i, we have the first half that we require.
Now, dont care about the second half and add to the solution variable(initiated to zero) a value equal to end[i+2]. end[i+2] tells us the total number of ways starting from i+2 till the end, to get a sum equal to sum/3 for the third part.
Here, what we have done is taken care of the first and the third part, doing which we have also taken care of the second part which will be by default equal to sum/3. Our solution variable will be the final answer to the problem.
Given below are the code snippets for better understanding of the above mentioned algorithm::-
Here we are doing the backward scanning to store the number of ways to get sum/3 from the end for each index.
long long int *end = (long long int *)calloc(numbers, sizeof(long long int);
long long int temp = array[numbers-1];
if(temp==sum/3){
end[numbers-1] = 1;
}
for(i=numbers-2;i>=0;i--){
end[i] = end[i+1];
temp += array[i];
if(temp==sum/3){
end[i]++;
}
}
Once we have the end array we do the forward loop and get our final solution
long long int solution = 0;
temp = 0;
for(i=0;i<numbers-2;i++){
temp+= array[i];
if(temp==sum/3){
solution+=end[i+2];
}
}
solution stores the final answer i.e. the number of ways to split the array into three contiguous parts having equal sum.

How does this bubblesort variation effectively carry out a sort?

// A strange variation on a bubblesort I created inadvertently. I omitted the usual if a[j] >a[j+1] by mistake yet the code was still able to function perfectly. Would there be any advantage to using a bubblesort of this kind over a normal bubblesort.
public int[] bubbleSort(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
for (int j = i + 1; j < a.length - 1; j++)
{
if (a[i] > a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}
Notice that at the end of the first iteration of the outer loop, the first value in the array will necessarily be the minimum value in the array (do you see why?) After the second iteration, the second value will be the second-smallest value, and after the third iteration the third value will be the third-smallest value, etc.
(That said, I think there's a bug in your logic. The upper bound on j should be a.length rather than a.length - 1, since otherwise the last value in the array is never compared to anything else or moved.)
You might want to look into selection sort, which works by moving the smallest value in the array to the front, then the second-smallest, etc. The algorithm you've come up with is (essentially) a modified version of selection sort rather than a modified bubble sort.
Hope this helps!

Keep track of the lowest numbers in an array

I am trying to keep track of the the scores of the lowest numbers and if I find the lowest scores of those players I don't want them to play again in the next round. I have gotten to the point of storing those low player value into the array but I only want them to be stored ONCE.
for(int i =0; i < player.length; i++){
for(int j =1; j < player.length; j++){
if(player[j] < player[i]){
min[i] =j;
System.out.println(min[i]+" "+round+" "+playerList.get(j));
}
}
}
One way is to have a sorted array, but it might be an overhead as far as insertion into the array is concerned.
The other method is to encapsulate the array in a data structure which internally keeps track of the index of the lowest value in the array. This data structure will have special methods for insertion and deletion which would always check while inserting and deleting to update the private member if the new number to be inserted is lower than the current lowest number.
This data structure should also expose a method to return the index of the lowest number in the array, which is already stored in a member variable.
Do 2 separate loops instead. One to find lowest number, second to collect indexes.
int minValue = 1000000; //
for(int i =0; i< player.length; i++){
if(player[i] < minValue){
minValue = player[i];
}
}
int j =0;
for(int i =0; i< player.length; i++){
if(player[i]==minValue){
min[j]=i;
j++;
}
}
One way to sort lowest numbers in array
// let array be of size x
int arr[]=new int[x];
// Now,assign some values to array
int smallest=arr[0]; // assign any value to smallest
// logic
for( int i=0; i < arr.length; i++ ) {
if( smallest > arr[i] ) {
smallest = arr[i];
}
}
System.out.println(smallest); // gets the smallest number out on output stream
Unless this is homework, you should just have an Object for each player and score. You can add them to a PriorityQueue to always get the lowest or add them to a Collection/array[] and call sort().
You may want to keep just one copy, but that makes more work for yourself and only saves the computer a milli-second at most.
Your time is worth more than the computers most of the time.

Categories

Resources