So I made this sortion sort method, all using for loops.
I revised it on white board over and over, it looks perfect, however, when I implemented it, it keeps giving me wrong sort, but if I reversed the if condition, it will give me the right answer but in reverse, this doesn't make sense!
public void insertionSort(){
for (int i = 1; i < items.length; i++){
for (int j = 0; j < i; j++){
if (items[i] < items[j]) {
int temp = items[i];
shift(items,j, i-1);
items[j] = temp;
}
}
}
}
private void shift(int[] array, int index1, int index2){
if (index1 == index2)
array[index1 + 1] = array[index1];
else{
for (int i = index2; i >= index1; i--)
array[i+1] = array[i];
}
}
Thanks for the input, I discovered the problem in my Array class, it simply doubles the array size, my mistake was that I used the array.length instead of count.
public void insertionSort(){
for (int i = 1; i < count; i++){
for (int j = 0; j < i; j++){
if (items[i] < items[j]) {
int temp = items[i];
shift(items,j, i-1);
items[j] = temp;
}
}
}
}
Related
This method is supposed to sort the words from a given file in alphabetical order after it is selected. Everything is working except it doesn't properly sort it. The input file reads "kundu is a man kundu man", but no matter what I try I get "[is, kundu, a, man, kundu, man]".
I tried taking away the "-1" and the "+1" but that did nothing to help.
private String[] selectionSort(String[] stringArray)
{
for(int j = 0; j < stringArray.length - 1; j++)
{
int min = j;
for(int k = j + 1; k < stringArray.length; k++)
{
if(stringArray[k].compareTo(stringArray[min]) < 0)
min = k;
swap(stringArray, j, min); //this method swaps the words
// by using a temp
//swap(intArray, j, min);
}
}
return stringArray;
}
private void swap(String [] stringArray, int i, int j) //swap method
{
String temp = stringArray[i];
stringArray[i] = stringArray [j];
stringArray[j] = temp;
}
Your swap call should be after the inner loop. Like,
private String[] selectionSort(String[] stringArray) {
for (int j = 0; j < stringArray.length - 1; j++) {
int min = j;
for (int k = j + 1; k < stringArray.length; k++) {
if (stringArray[k].compareTo(stringArray[min]) < 0) {
min = k;
}
}
swap(stringArray, j, min);
}
return stringArray;
}
After that, with no other changes and your input, I get
[a, is, kundu, kundu, man, man]
I was reading up on sorting algorithms, I finished selection and bubble sort and thought I should try to implement what I understood.It took me while to understand what I wrote intending to be selection sort(code snippet-1) wasn't implementing key features of selection sort at all(which is finding min of unsorted array and building sorted array one element at a time). So I wrote one more for selection sort(code snippet-3). But now, I'm curious about Snippet-1. Can someone tell me if it is bubble sort or not?
Code Snippet-1
public void sort(int[] arr) {
// code snippet-1
int n = arr.length;
for (int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(arr[i] > arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
Code Snippet-2
public void sort(int[] arr) {
// code snippet-2
int n = arr.length;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-1; j++) {
if(arr[j] > arr[j+1]){
int temp=arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
Code Snippet-3
public void sort(int[] arr) {
// Code snippet-3
int n = arr.length;
for (int i = 0; i < n; i++) {
int min = i;
for(int j = i + 1; j < n; j++){
if(arr[j] < arr[min]) {
min = j;
}
}
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
Also, on a not so related account, can someone explain how the outer for loop condition (i.e i<n and i<n-1) doesn't affect the result in these cases? I only changed snippet-2's condition to j<n-1 as it gave me Arrayoutofbound error because of arr[j+1] term. Yes, I saw the entire thing in debug mode and animations too but still not completely clear how to choose a condition. I know I'm missing something here.
You have different approaches to bubble sort and the Arrayoutofbound exception is because of the comparison of n+1 element which is not there in the array.
In code snippet 2, you could also avoid one more loop by doing:
for (int i = 0; i <= n-2; i++)
Using selection sort to sort an array. I think my logic is right but there's this stupid error.
public static void arraySort(int[] a) {
//for loop to go through array
for(int i = 0; i < a.length; i++) {
int temp = a[i]; //set a temp value for first value
for (int x = i + 1; x < a.length; x++) {
if (a[x] < temp) {
a[i] = a[x];
temp = a[x];
}
}
}
}
At the end it keeps printing only one group of values repeatedly.
You should track the minimum index in the outer loop and swap two elements after each inner loop completion, e.g.:
for (i = 0; i < size - 1; i++) {
minIndex = i;
for (j = i; j < size; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
swap(array, i, minIndex);
}
Your fixed code:
public static void arraySort(int[] a) {
//for loop to go through array
for(int i = 0; i < a.length; i++) {
int temp = a[i]; //set a temp value for first value
int minIndex = i;
for (int x = i + 1; x < a.length; x++) {
if (a[x] < temp) {
a[i] = a[x];
temp = a[x];
minIndex = x;
}
}
//swap
a[minIndex] = a[i];
a[i] = temp;
}
}
Recently, I took Linkedin placement test in which there was a question in which output for 4 test cases were wrong for me. I could not figure out what was my mistake becasue inputs/outputs were hidden.
Anyways here was the question:
Find the maximum element from an array where product of any other two elements would be equal to that number and return that number .If no, such element is there then return -1.
Here was my solution:
static int maxElement(int[] arr) {
Arrays.sort(arr);
int max = arr[arr.length-1];
int result = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
result = arr[i] * arr[j];
if (result == max) {
return max;
}
}
}
return -1;
}
I guess you need to find the possible maximum number in the array and the product of two elements in the array.
If I assume this, your code fails for this test case:
int[] arr = {2,4,5,3,7,6}; , where the answer should be 6
Check this below code it will work for above test-case.
Just add one more reverse for loop to check the possible value and product.
static int maxElement(int[] arr) {
Arrays.sort(arr);
for (int k = arr.length-1; k >= 0; k--) {
int max = arr[k];
int result = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
result = arr[i] * arr[j];
if (result == max) {
return max;
}
}
}
}
return -1;
}
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < a.length; i++) {
list.add(a[i]);
}
int maxSum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if ((a[i] * a[j]) > maxSum) {
if(list.contains(a[i] * a[j]))
maxSum = a[i] * a[j];
}
}
}
if (maxSum != 0)
return maxSum;
return -1;
I need to write an algorithm that will take an array of ints and find the k'th largest element in the array. The caveat here is that the runtime must be O(K*n) or better.
My teacher has made it clear this can be done with a modified bubble sort program, but I am unsure as to how I can modify the bubble sort without ruining it, as I would think it necessary to loop through every element of the array. Here is my code (just the shell of the program and an unmodified bubble sort):
public int kthLargest(int[] A, int k){
int[] sorted = A;
int temp;
for (int i = (A.length - 1); i >= 0; i--)
{
for (int j = 1; j <= i; j++)
{
if (sorted[j-1] < sorted[j])
{
temp = sorted[j-1];
sorted[j-1] = sorted[j];
sorted[j] = temp;
}
}
}
return sorted[k-1];
}
Figured it out:
public int kthLargest(int[] A, int k){
int[] sorted = A;
int temp;
for (int i = 0; i < A.length-1; i++)
{
for (int j = 0; j < A.length-i-1; j++)
{
if (sorted[j] > sorted[j+1])
{
temp = sorted[j];
sorted[j] = sorted[j+1];
sorted[j+1] = temp;
}
if(i == (k-1)) return A[A.length-i-1];
}
}
return sorted[A.length-k];
}
The the array has been sorted to the k-th largest element, it has found what it is looking for and can stop the sort and return.
I thought a modified bubble sort just exited early if the list was already sorted?
Wouldn't something like this suffice?
public int kthLargest(int[] A, int k){
int[] sorted = A;
int temp;
bool bSorted = TRUE;
for (int i = (A.length - 1); i >= 0; i--)
{
for (int j = 1; j <= i; j++)
{
if (sorted[j-1] < sorted[j])
{
temp = sorted[j-1];
sorted[j-1] = sorted[j];
sorted[j] = temp;
bSorted = FALSE;
}
}
if(bSorted)
break;
}
return sorted[k-1];
}