I want to sort an array of int from the smallest value to the greatest one. I have created the next algorithm but the problem is that the new array does't receive the right values from the if statement. I am going put my code bellow.
public static void main(String[] args) {
int[] arr = {33,44,22,11,22,11};
int[] arrSort = new int[6];
int temp=0;
for (int i = 0; i < arrSort.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
temp = arr[i + 1];
arr[i + 1] = arr[i];
arrSort[i] = temp;
}
else {
arrSort[i] = arr[i];
}
}
System.out.println(Arrays.toString(arrSort));
}
If i run the code I get the next values: [33, 22, 11, 22, 11, 0];
I just want to figure out what part of the algorithm was thought wrong. Thank you in advance.
You need to apply 2 loops.
1st loop is to access 1st arr element.
2nd loop is to access next(1st + 1)th element.
After comparing 1st element with other swap it accordingly.
public static void main(String []args)
{
int[] arr = {33,44,22,11,22,11};
int len=arr.length;
int temp=0,i,j;
for (i = 0; i < len; i++)
{
for (j = i+1; j < len; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(i=0; i<arr.length; i++)
{
System.out.println(arr[i]);
}
}
when it is the case of array Loops are very handy to use.
int[] yourArary = new int[10];
ArrayList<Integer> intArray = new ArrayList<>();
for( int value : yourArary ){
intArray.add( value );
}
Arrays.sort(intArray);
System.out.println(Arrays.toString(yourArary));
// you can short like this in reverse order
for (int i = yourArary.length - 1; i >= 0; i--)
System.out.print(yourArary[i] + " ");
System.out.println();
You cannot do it with just one loop. Sorting is a more complex than that. For bubble sort or selection sort it's like O(n^2). There are better algorithms that like quick sort or merge sort that have better results and aim for O(n log N) complexity. But anyway you can do it like that for example for a simple bubble sort implementation:
int[] arr = {33,44,22,11,22,11};
for (int i = 0; i < arrSort.length - 1; i++) {
for(int j=i;j<arrSort.length; j++) {
if (arr[i] > arr[j]) {
temp = arr[j];
arr[j] = arr[i];
arr[i]=temp;
}
}
System.out.println(Arrays.toString(arr));
Don't know if this will help you, but why sort it by yourself, if Java can do the Job:
int[] unsortedArray = { 33, 44, 22, 11, 22, 11 };
ArrayList<Integer> intArray = new ArrayList<>();
for( int value : unsortedArray )
{
intArray.add( value );
}
Collections.sort( intArray );
System.out.println( intArray );
If you use the helper variable temp to move positions, you don't need a second array, just put it back into arr[i]. Second, one run is not enough, you will need to run this, until there are no position changes needed. So it would look like this:
public static void main(String[] args) {
//source Array
int[] arr = {33,44,22,11,22,11};
int temp=0;
//marker, that positions were switched
boolean sthChanged = false;
do
{
//in each run assume that nothing is left to change
sthChanged = false;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
//we found an instance, where the earlier position holds a higher int than the latter
//save the latter value in the temp variable
temp = arr[i + 1];
//move the former value to the latter position
arr[i + 1] = arr[i];
//fill the former position with the value from the temp variable
arr[i] = temp;
//set the marker to true, as there might be other changes to be done
sthChanged = true;
}
}
}
while (sthChanged); //stop only, if we didn't find sth to be changed in the last run
System.out.println(Arrays.toString(arr));
}
Best regards
First of all, u shud not use two arrays in your algorithm, as its just a waste of memory and unecessary array access overhead. If u need to retain the original array copy it over, and send the copied array to the sort method.
Secondly, for the method adopted here (bubble sort), you are iterating the array and finding the largest element and shifting it to the end.
U need to repeat this process, for each subarray of length-1, length-2 ... 1.
So the time complexity of this approach would be O(n^2). There are better algorithms, like MergeSort, QuickSort which can do the sort in the theoretical O(n log(n)) time.
See below the correctin required for your code :
public static void main(String[] args) {
int[] arr = {33,44,22,11,22,11};
//int[] arrSort = new int[6];
int temp=0;
for(int j = 0; j < arr.length; j++) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
temp = arr[i + 1];
arr[i + 1] = arr[i];
arr[i] = temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
Related
I have made it so that I get the elements that cause the list to be unordered. But how to delete them, and then display the list without them?
public class Main16 {
public static void main(String[] args){
int arr[] = { 1, 2, 3, 7, 4, 5, 9, 6};
int n = arr.length;
printElements(arr, n);
}
static void printElements(int arr[], int n)
{
for (int i = 1; i < n - 1; i++)
{
if (arr[i] > arr[i + 1])
System.out.print(arr[i] + " ");
}
}
}
This is definitely not the best way to sort arrays. The most optimized way to sort would be to convert your array to a Collection and use Collections.sort.
However - tried the below, using ArrayList to store the values to keep. (Also note, comparing as arr[i] > arr[i + 1] will exclude the last element ∴ added a dummy value.)
import java.util.ArrayList; import java.util.Arrays; import java.util.List;
public class Trial {
public static void main(String[] args) {
int arr[] = {1,2,3,7,4,5,9,6};
int n = arr.length;
int newarr[] = new int[n + 1];
for (int i = 0; i < n; i++)
newarr[i] = arr[i];
newarr[n] = 1948; //Dummy Value Appended
printElements(newarr, n + 1);
}
static void printElements(int arr[], int n) {
//Define an ArrayList to store the values that would not be removed
List < Integer > valuesToKeep = new ArrayList < Integer > (Arrays.asList());
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
System.out.print("Removing item: " + arr[i] + "\n");
} else {
System.out.print("Keeping item: " + arr[i] + "\n");
valuesToKeep.add(arr[i]); //Add
}
}
Object[] valuesToKeep_array = valuesToKeep.toArray();
//Print
System.out.print("\nFinal Sorted Array: ");
for (int i = 0; i < valuesToKeep_array.length; i++) {
System.out.print(valuesToKeep_array[i]);
}
}}
As Nexevis said, you can't really do what you want to do with arrays. You have 2 options:
Go through the process of working with arrays and copying and transferring.
1.5) You can also use java.util.Arrays.sort() if you just want an array that's in the right order, but it seems like you want to remove them outright:
Arrays.sort(arr);
Use a List<Integer> because all you have to do is find the incorrect terms and then remove() them from the list. The length will automatically adjust. (I'd be happy to demonstrate how you'd do this if you want)
I'm working on this project using Arrays. I have a method called createRandomIntArray that creates an array. This method is meant to return the Array in descending order. I have been able to do just that but I want to know if there is a more effective way to write this method than the way I wrote it. I have my code below.
public static int[] createRandomIntArray(int n) {
Random random = new Random();
int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = random.nextInt(n);
}
Arrays.sort(result);
for (int i = 0; i < result.length / 2; i++) {
int temp = result[i];
result[i] = result[result.length - i - 1];
result[result.length - i - 1] = temp;
}
return result;
}
You could avoid reversing the sorted array by directly sorting reverse order:
Arrays.sort(result, Comparator.reverseOrder());
As mentioned in the comments one could use Random.ints(…) if you’re not tied to using arrays:
random.ints(n, 0, n).boxed().sorted(Comparator.reverseOrder()).mapToInt(i -> i).toArray();
Using Java Streams
private static int[] createRandomIntArray(int n) {
return ThreadLocalRandom.current().ints() // Stream of random ints
.limit(n) // Limit the stream to n values
.boxed() // Convert to Stream of Integer Objects for reverse sorting
.sorted(Collections.reverseOrder()) // Sort in reverse Order
.mapToInt(Integer::intValue) // Map back to primitive ints
.toArray(); // as Array
}
public class SortDes {
public static void main(String[] args) {
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
System.out.println("Elements of array sorted in descending order: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
There's an alternative which is using Stream.
Arrays.stream(result).sorted(Comparator.reverseOrder());
starting Java coder here. I was wondering how to change my code so it would sort array by always swapping the biggest value to first. Sample output should be:
[3, 1, 2, 0] [3, 2, 1, 0].
public class Sorting {
static void biggest(int[] arr, int start, int end) {
for (start = 0; start < arr.length; start++) {
for (end = start + 1; end < arr.length; end++) {
if (arr[start] < arr[end]) {
int temp = arr[end];
arr[end] = arr[start];
arr[start] = temp;
System.out.println(Arrays.toString(arr));
}
}
}
}
public static void main(String[] args) {
int[] arr = {0, 1, 2, 3};
int temp = 0;
for (int i = 0; i < 4; ++i) {
biggest(arr, temp, 4 - 1);
for (int j = 0; j < 4; ++j) {
}
++temp;
}
}
Thanks in advance,
- Em
If you just want the sort to be successful, I suggest taking advantage of Java's built in sort method then reversing the list as suggested here:
Arrays.sort(arr);
ArrayUtils.reverse(arr);
But it sounds like the spirit of your question is to modify your code for this purpose. Here's the solution I came up with:
import java.util.Arrays;
public class Sorting {
static void biggest(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(Arrays.toString(arr));
int max, maxAt = i;
for (int j = i; j < arr.length; j++) {
maxAt = arr[j] > arr[maxAt] ? j : maxAt;
}
max = arr[maxAt];
if (arr[i] < max) {
arr[maxAt] = arr[i];
arr[i] = max;
}
}
}
public static void main(String[] args) {
int[] arr = {0, 1, 2, 3};
biggest(arr);
System.out.println(Arrays.toString(arr));
}
}
First off, you had a lot of extra code that you didn't need. It's a bad idea to have a loop in your main. That should be handled by the helper function. You also had a lot of redundant declarations (like start and end). You were on the right track with your helper function, but because of your main loop your time complexity was 0(n²). Eliminating that allows mine to be O(logn). Complexity aside, the key difference in terms of logic is here in your internal loop:
for (end = start + 1; end < arr.length; end++) {
if (arr[start] < arr[end]) {
int temp = arr[end];
arr[end] = arr[start];
arr[start] = temp;
In this loop you're switching the array entry with the first one you find that's bigger. This will result in unwanted early switches (like 1 & 2). Here is my solution:
for (int j = i; j < arr.length; j++) {
maxAt = arr[j] > arr[maxAt] ? j : maxAt;
}
max = arr[maxAt];
if (arr[i] < max) {
arr[maxAt] = arr[i];
arr[i] = max;
}
The key difference is that I search for the maximum value entry following the one we're swapping. That way as we proceed through the array we will always bring forward the next biggest.
Best of luck learning Java I hope this helps!
I have a task, to remove duplicates in array, what by "remove" means to shift elements down by 1, and making the last element equal to 0,
so if I have int[] array = {1, 1, 2, 2, 3, 2}; output should be like:
1, 2, 3, 0, 0, 0
I tried this logic:
public class ArrayDuplicates {
public static void main(String[] args) {
int[] array = {1, 1, 2, 2, 3, 2};
System.out.println(Arrays.toString(deleteArrayDuplicates(array)));
}
public static int[] deleteArrayDuplicates(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] == array[j]) { //this is for comparing elements
for (; i > 0; i--) {
array[j + 1] = array[j]; //this is for shifting
}
array[array.length - 1] = 0; //making last element equal to "0"
}
}
}
return array;
}
}
But it doesn't work.. Is anyone familiar with a right solution?
I appreciate your assistance and attention very much.
Your Code:
In short, the approach you have chosen calls for a third loop variable, k, to represent the index that is currently being shifted left by 1 position.
i - the current unique item's position
j - the current position being tested for equality with unique item at i
k - the current position being shifted left due to erasure at j
Suggestion:
A more efficient approach would be to eliminate the repetitive left shifting which occurs each time a duplicate is found and instead keep track of an offset based on the number of duplicates found:
private static int[] deleteArrayDuplicates(int[] array) {
int dupes = 0; // total duplicates
// i - the current unique item's position
for (int i = 0; i < array.length - 1 - dupes; i++) {
int idupes = 0; // duplicates for current value of i
// j - the current position being tested for equality with unique item at i
for (int j = i + 1; j < array.length - dupes; j++) {
if (array[i] == array[j]) {
idupes++;
dupes++;
} else if(idupes > 0){
array[j-idupes] = array[j];
}
}
}
if(dupes > 0) {
Arrays.fill(array, array.length-dupes, array.length, 0);
}
return array;
}
This has similar complexity to the answer posted by dbl, although it should be slightly faster due to eliminating some extra loops at the end. Another advantage is that this code doesn't rely on any assumptions that the input should not contain zeroes, unlike that answer.
#artshakhov:
Here is my approach, which is pretty much close enough to what you've found but using a bit fewer operations...
private static int[] deleteArrayDuplicates(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
if (array[i] == NEUTRAL) continue; //if zero is a valid input value then don't waste time with it
int idx = i + 1; //no need for third cycle, just use memorization for current shifting index.
for (int j = i + 1; j < array.length; j++) {
if (array[i] == array[j]) {
array[j] = NEUTRAL;
} else {
array[idx++] = array[j];
}
}
}
return array;
}
I just wrote the following code to answer your question. I tested it and I am getting the output you expected. If there are any special cases I may have missed, I apologize but it seemed to work for a variety of inputs including yours.
The idea behind is that we will be using a hash map to keep track if we have already seen a particular element in our array as we are looping through the array. If the map already contains that element- meaning we have already seen that element in our array- we just keep looping. However, if it is our first time seeing that element, we will update the element at the index where j is pointing to the element at the index where i is pointing to and then increment j.
So basically through the j pointer, we are able to move all the distinct elements to the front of the array while also making sure it is in the same order as it is in our input array.
Now after the first loop, our j pointer points to the first repeating element in our array. We can just set i to j and loop through the rest of the array, making them zero.
The time complexity for this algorithm is O(N). The space complexity is O(N) because of the hash table. There is probably a way to do this in O(N) time, O(1) space.
public static int[] deleteArrayDuplicates(int[] array) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int j = 0;
for (int i = 0; i < array.length; i++) {
if (map.containsKey(array[i])) {
continue;
}
else {
map.put(array[i],1);
array[j] = array[i];
j++;
}
}
for (int i = j; i < array.length; i++) {
array[i] = 0;
}
return array;
}
Let me know if you have additional questions.
Spent a couple of hours trying to find a solution for my own, and created something like this:
public static int[] deleteArrayDuplicates(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[j] == array[i]) { //this is for comparing elements
int tempIndex = j;
while (tempIndex + 1 < array.length) {
array[tempIndex] = array[tempIndex + 1]; //this is for shifting elements down/left by "1"
array[array.length - 1] = 0; //making last element equal to "0"
tempIndex++;
}
}
}
}
return array;
}
Code is without any API-helpers, but seems like is working now.
Try this:
public static void main(String[] args)
{
int a[]={1,1,1,2,3,4,5};
int b[]=new int[a.length];
int top=0;
for( int i : a )
{
int count=0;
for(int j=0;j<top;j++)
{
if(i == b[j])
count+=1;
}
if(count==0)
{
b[top]=i;
top+=1;
}
}
for(int i=0 ; i < b.length ; i++ )
System.out.println( b[i] );
}
Explanation:
Create an another array ( b ) of same size of the given array.Now just include only the unique elements in the array b. Add the elements of array a to array b only if that element is not present in b.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class StackOverFlow {
public static void main(String[] args) {
int[] array = {1, 1, 2, 2, 3, 2};
Set<Integer> set=new HashSet<>();
for (int anArray : array) {
set.add(anArray);
}
int[] a=new int[array.length];
int i=0;
for (Integer s:set) {
a[i]=s;
i++;
}
System.out.println(Arrays.toString(a));
}
}
Hope this simple one may help you.
Make use of Set which doesn't allow duplicates.
We can use ARRAYLIST and Java-8 Streams features to get the output.
public static int[] deleteArrayDuplicates(int[] array) {
List<Integer> list = new ArrayList(Arrays.stream(array).boxed().distinct().collect(Collectors.toList()));
for (int i = 0; i < array.length; i++) {
if (i < list.size()) {
array[i] = list.get(i);
} else {
array[i] = 0;
}
}
return array;
}
OUTPUT
[1, 2, 3, 0, 0, 0]
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]);
}