I want to count the minimum number of steps to sort an array .(A STEP IS 'COUNTED' BY THE NUMBER OF TIMES IT MOVED FROM IT'S ORIGINAL POSITION TO IT'S FINAL POSITION).
Eg. [5,2,1,3,5] : count = 4 as 2 moves left one time, then 1 moves left two times and then 3 moves left 1 time to form [1,2,3,5,5].
[3,5,2,3] : count = 3 as 2 moves left two times and 3 moves left one time to form [2,3,3,5]
#sort an array
1 messages · Page 1 of 1 (latest)
<@&987246717831381062> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
List<Integer> list = Arrays.stream(numbers).sort().collect(Collectors.toList());
Should i explain it? I think its obvious. You get your array of numbers, sort them and cast the stream to a list
Well not cast but you got it, not know the right word for it
Oh wait i miss read your question
You not just want to sort it
Its not solving the problem. They want to count the steps that were required for sorting the array.
You first need to tell us, what sorting algorithm you want to use @daring fable
Track the ints that need to be moved, and choose the next element to move based on its position in the array
Something like this: ```Java
public int countSteps(int[] nums) {
int n = nums.length;
int[] sorted = nums.clone();
Arrays.sort(sorted);
int[] index = new int[n];
for (int i = 0; i < n; i++)
index[i] = Arrays.binarySearch(sorted, nums[i]);
int count = 0;
boolean[] visited = new boolean[n];
for (int i = 0; i < n; i++) {
if (visited[i] || index[i] == i)
continue;
int cycleSize = 0;
int j = i;
while (!visited[j]) {
visited[j] = true;
j = index[j];
cycleSize++;
}
count += cycleSize - 1;
}
return count;
}
But try it ur self and not copy this code
@woven geode how do i format this
it's backsticks
public class MinStep {
public static void main(String[] args) {
int arr[]={3,5,2,3};
System.out.println(minSteps(arr));
for(int i=0;i< arr.length;i++)
System.out.print(arr[i]);
}
public static int minSteps(int []arr)
{int count =0;
for(int i=0;i< arr.length-1;i++)
{ int j=i;
int key = arr[j+1];
while(j>=0&&key<arr[j])
{
arr[j+1]=arr[j];
j--;
count++;
}
arr[j+1]=key;
}
return count;
}
}
Detected code, here are some useful tools:
Formatted code
public class MinStep {
public static void main(String[] args) {
int arr[] = {
3, 5, 2, 3};
System.out.println(minSteps(arr));
for (int i = 0; i < arr.length; i++) System.out.print(arr[i] );
}
public static int minSteps(int [] arr) {
int count = 0;
for (int i = 0; i < arr.length - 1; i++) {
int j = i;
int key = arr[j + 1] ;
while (j >= 0 && key < arr[j] ) {
arr[j + 1] = arr[j] ;
j--;
count++;
}
arr[j + 1] = key;
}
return count;
}
}