I have the following code snippet..
public void selsort()
{
int j=0,i,k; int low;
for(k = 0; k < ele; k++)
{
for(i = j;i < ele; i++)
if(a[i] < a[i+1])
low = i;
if(i!=j)
{
long temp=a[j];
a[j]=a[i];
a[i]=temp;
}
j++;
}
}
I don't know why the above algorithm is not working properly.
What has to be modified to get it working properly?
fixed your code
public void selsort() {
int j = 0, i, k;
int low;
for (k = 0; k < ele; k++) {
low = j;
for (i = j; i < ele; i++)
if (a[i] < a[low]) {
low = i;
}
if (j != low) {
int temp = a[j];
a[j] = a[low];
a[low] = temp;
}
j++;
}
}
but can be used k insdead of j, and better have input params instead using class fields like a and ele
Related
My methods below are to fix the heap order.
private void upHeap(int i) {
// TO DO Implement this method
int temp = 0;
int n = heap.length-1;
for(int j=n;j>0;j--){
if(heap[j]>heap[parent(j)]){ //if current index is greater than its parent, swap
temp = heap[j]; //use a temporary variable to help
heap[j] = heap[parent(j)];
heap[parent(j)] = temp;
upHeap(heap[parent(j)]);
}
}
}
And the down heap
private void downHeap(int i) {
// TO DO Implement this method
int temp = 0;
for(int j=i; j<heap.length; j++){
if(heap[i]<heap[j]){
temp = heap[j];
heap[j] = heap[i];
heap[i] = temp;
}
}
}
It is a maxHeap, so the numbers should be descending. Can anybody see in my code where I've gone wrong? It is now giving me an index out of bounds error.
Try these:
private void upHeap(int i) {
int temp = 0;
for (int j = i; j >= 0; j--) {
for (int k = j - 1; k >= 0; k--) {
if (heap[j] > heap[k]) {
temp = heap[j];
heap[j] = heap[k];
heap[k] = temp;
} else {
break;
}
}
}
}
private void downHeap(int i) {
int temp = 0;
for (int j = i; j < heap.length; j++) {
for (int k = j + 1; k < heap.length; k++) {
if (heap[k] > heap[j]) {
temp = heap[j];
heap[j] = heap[k];
heap[k] = temp;
} else {
break;
}
}
}
}
When my sort is run on this data {7,8,4,2,3,9,5,8,4,1} only the first element is not put in its correct place. How can I fix this? Thanks for the help.
public void segmentedInsertionSort(int[] array, int size, int h)
{
int temp;
for(int i = h + 1 ;i < size;i++)
{
int j = i - h;
while(j > 0)
{
if(array[j+h] < array[j])
{
temp = array[j];
array[j] = array[j+h];
array[j+h] = temp;
j = j - h;
}
else
{
j = 0;
}
}
}
}
public void shellSort(int[] array, int size)
{
int h = size/2;
while(h > 0)
{
segmentedInsertionSort(array,size,h);
h = h/2;
}
}
for(int i = h + 1 ;i < size;i++)
{
int j = i - h;
while(j > 0)
{
if(array[j+h] < array[j])
{
temp = array[j];
array[j] = array[j+h];
array[j+h] = temp;
j = j - h;
}
In this part, you define i = h + 1 and then increase i value. So, so the j value is never less than 1 when the sort runs. Therefore, it never processes the first element of array. You need to fix this part.
I think you have misvalued some variables.Your variable j never reaches index 0 so as to compare 7 with any other value in this Comparison Sort.
Change:
for(int i = h +1 ;i < size;i++) to for(int i = h ;i < size;i++)
And
while(j > 0) to while(j >= 0)
else
{
j = 0;
}
to
else{
j = -1;
}
Final Code looks like:
` void segmentedInsertionSort(int arr[], int size, int h)
{
int temp;
for(int i = h ;i < size;i++)
{
int j = i-h ;
while(j >= 0)
{
if(arr[j+h] < arr[j])
{
temp = arr[j];
arr[j] = arr[j+h];
arr[j+h] = temp;
j = j - h;
}
else
{
j = -1;
}
}
}
}
void shellSort(int arr[], int size)
{
int h = size/2;
while(h > 0)
{
print(arr);
segmentedInsertionSort(arr,size,h);
h = h/2;
}
}
I've written this to sort two arrays and then compare the values to see if they're the same, but it always returns false, and I can't figure out why.
It is supposed to find if two arrays are permutations of each other.
public class Permutations {
public static void main(String[] args) {
int[] a = {1,4,6,7,8,34};
int[] b = {34,6,8,1,4,7};
System.out.println(arePermutations(a, b));
}
public static boolean arePermutations(int[] a, int[] b)
{
int count = 0;
int temp = 0;
if(a.length == b.length)
{
for(int i=0; i<a.length-1; i++)
for(int j=0; j<a.length-1; j++)
if(a[i] > a[j+1] && i>j+1)
{
temp = a[i];
a[i] = a[j+1];
a[j+1] = temp;
}
{
for(int i=0; i<b.length-1; i++)
for(int j=0; j<b.length-1; j++)
if(b[i] > b[j+1] && i>j+1)
{
temp = b[i];
b[i] = b[j+1];
b[j+1] = temp;
}
}
for(int i=0; i<a.length; i++)
if(a[i] == b[i])
{
count++;
}
if (count == a.length)
{
return true;
}
else return false;
}
else return false;
}
}
The problem is in the implementation of the bubble sort. Change your sorting loops to the following:
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length - 1; j++) {
if (b[j] > b[j + 1]) {
temp = b[j];
b[j] = b[j + 1];
b[j + 1] = temp;
}
}
}
This code works because a bubble sort just swaps adjacent elements if they need swapping, but needs to run through the entire array multiple times (up to the number of items in the array) to get all the items into the correct position.
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;
}
}
Here is my array code:
int h = 0;
int i = 1;
while (h < a.length - 1){
if (a[h] < a[i]){
h++;
i++;
}
if (a[h] > a[i]){
a[i] = a[h];
a[h] = a[i + 1];
}
}
if (h == a.length - 1){
System.out.println(a[i]);
}
I don't know what I'm doing wrong. I need the answer for a school assignment. I'm in my school's computer science class.
check this out, i'm not sure what language are you using, but i would choose to use for loop instead of while
int h = 0;
int i = 0;
int v = null;
while (h < a.length - 1){
if(v == null || v < a[i])
{
v = a[i];
h = i;
}
i++;
}
if (v != null){
System.out.println(v); //for value
System.out.println(h); //highest index
System.out.println(a[h]); //for value from array a
}
int max = a[0];
int index = 0;
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
index = i;
}
}
Try to create this method
public int max(int[] a) {
int temp = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > temp) {
temp = a[i];
}
}
return temp;
}
and in your main method
int[] a = new int[3];
a[0]=2;
a[1]=3;
a[2]=54;
System.out.println("Highest value is "+ max(a));
Arrays.sort(a);
System.out.println(a[a.length - 1]);