Insertion Sort String Array, not sorting right - arrays

Hello this is my method for a String Array insertion sort. It is returning bogus results to the console. Mainly just one array element over and over. Any help what needs to be changed is greatly appreciated.Thank you.
public static void insertionSort(String[] a, int count) {
int i, j;
String Value;
for (i = 1; i < count; i++) {
Value = a[i];
j = i - 1 ;
while (j >= 0 && a[j].compareTo(Value)> 0) {
a[j+1] = a[j];
j=j-1;
}
a[i+1] = Value;
}
}

The code below works.
public static void insertionSort(String[] a, int count) {
int i, j;
String Value;
for (i = 1; i < count; i++) {
Value = a[i];
j = i;
while (j > 0 && a[j-1].compareTo(Value)> 1) {
a[j] = a[j-1];
j--;
}
a[j] = Value;

The problem is a[j+1] = a[j];
Your original a[j+1] would be lost, you just simply replace it, without moving it / storing it, it is just simply replaced by a[j] and get lost...
I would modify your code like the following:
Find the position of current value to be inserted, swap it with the element at that position, then swap back that element into correct position
public static void insertionSort(String[] a, int count) {
int i, j;
String Value;
for (i = 1; i < count; i++) {
Value = a[i];
j = i - 1 ;
int p = i;
while (j >= 0 && a[j].compareTo(Value)> 0) {
p = j--;
}
// p now is correct position to be inserted
swap(a[p], a[i]);
// Now loop the original a[p] back to a[p+1]
for(int z = i; z > p; z--){
swap(a[z], a[z-1]);
}
}
}

check the code works fine
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
printNumbers(array);
}
and to print the number i use function printNumbers(array)
private static void printNumbers(int[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println("\n");
}

Related

Why does the value go out of index of the array?

In bubble sort, when first iteration of the inner for loop takes place, value of i is 0, so the loop will run till j is less than n, but when j becomes equal to n-1, and we do a[j+1] for another time.
why doesn't this value go out of index of the array and the code executes properly?
for(i=0;i<n-1;i++) {
for(j=0;j<n-i;j++) { /* When i=0 & j<n why doesn't */
if(a[j] > a[j+1]) { /* a[j+1] go out of index when */
temp=a[j+1]; /* value of j is n-1 */
a[j+1]=a[j];
a[j]=temp;
}
}
}
Well this is a proper , Tested Bubble Sort function:
void bubble_sort(long a[], long n)
{
long i, j, t;
for (i = 0 ; c < ( n - 1 ); i++)
{
for (j = 0 ; j < n - i - 1; j++)
{
if (a[j] > a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
}
I think the problem is with you second for's, second argument.
It should run till j is less than i, not n.
void bubble(double *t, int db) {
int i, j;
for (i = db-1; i > 0; --i)
for (j = 0; j < i; ++j)
if (t[j+1] < t[j]) {
double temp = t[j];
t[j] = t[j+1];
t[j+1] = temp;
}
}

subscripted value is neither array nor pointer nor vector in c

how remove this error tried everything....
this program is about finding 5 closest number from a array...
in main part i simply take array, num and size and passes through the function
void printclosest(int arr[], int x, int n)
{
int diff[30];
int i,j,k,p,a;
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (arr[i] > arr[j])
{
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
}
}
for(i=0;i<n;i++)
{
diff[i]=abs(a[i]-x);
}
for (k = 0; k < n; ++k)
{
for (p = k + 1; p < n; ++p)
{
if (diff[k] > diff[p])
{
a = arr[k];
arr[k] = arr[p];
arr[p] = a;
}
}
}
for(i=0;i<5;i++)
{ printf("%d",arr[i]);
}
}
a is declared as int, yet you try to use it as an array here:
diff[i]=abs(a[i]-x);

Preventing duplicates from being stored when filling dynamic array with values from two different arrays

I need to know a method to keep duplicate numbers from being stored in a new array when taking numbers from two different arrays. The function is supposed to store each 'unique' value once and not store duplicate values again.
Here is my function code so far:
int * arrayIntersect(int *sizeOfResult, const int *a, const int *b, int sizeOfA, int sizeOfB){
int i;
int j;
int k = 0;
int c[(sizeOfA + sizeOfB)];
for(j = 0; j < sizeOfB; j++){
for(i = 0; i < sizeOfA; i++){
if(a[i] == b[j]){
c[k] = a[i];
(*sizeOfResult)++;
k++;
}
}
}
int *d = (int *)malloc(sizeof(int) * *sizeOfResult);
for(i = 0; i < *sizeOfResult; i++){
d[i] = c[i];
}
return d;
}
It prints the values I need, but I want to eliminate the same number from showing up multiple times when printing the contents of the new dynamic array.
Any idea on how to improve my code to allow prevent duplication?
The proper way to do it is having the arrays ordered and then doing a binary search for each insertion like #Murilo Vasoncelos pointed out.
Below is a quick and dirty solution that loops through a and b and for each iteration checks if the number has been inserted before. If it isn't, it inserts it.
int duplicate = 0;
*sizeOfResult = 0;
for(j = 0; j < sizeOfA; j++){
for(i = 0; i < (*sizeOfResult); i++){
if(c[i] == a[j]){
duplicate = 1;
break;
}
}
if (!duplicate)
{
c[(*sizeOfResult)] = a[i];
(*sizeOfResult)++;
}
duplicate = 0;
}
for(j = 0; j < sizeOfB; j++){
for(i = 0; i < (*sizeOfResult); i++){
if(c[i] == b[j]){
duplicate = 1;
break;
}
}
if (!duplicate)
{
c[(*sizeOfResult)] = b[i];
(*sizeOfResult)++;
}
duplicate = 0;
}
If your arrays a and b are ordered, you can simply use this linear algorithm for array intersection:
int* inter(int* szr, int* a, int* b, int sza, int szb)
{
int c[MAX(sza, szb)];
int i, j, k = 0;
for (i = 0, j = 0; i < sza && j < szb;) {
if (a[i] == b[j]) {
if (k == 0 || c[k - 1] < a[i]) {
c[k++] = a[i];
}
i++;
j++;
}
else if (a[i] < b[j]) {
i++;
}
else {
j++;
}
}
*szr = k;
int* ans = (int*)malloc(sizeof(int) * k);
for (i = 0; i < k; ++i) {
ans[i] = c[i];
}
return ans;
}

Out of bounds error in a loop

public static void bubbleSort(Farm a[], int n) {
String[] animals = getString(a);
for(int i = 0; i < n - 1; i++) {
boolean swapped = false;
for(int j = 0; j < n - 1 - i; j++) {
int c = animals[j].compareTo(animals[j+1]);
if(c > 0) {
swap(animals, j, j+1);
swapped = true;
}
}
if(!swapped)
break;
}
for (int i = 0; i < a.length; i++)
if(a[i] != null)
System.out.println(a[i].animal);
}
in int c = animals[j].compareTo(animals[j+1]); it is giving me an out of bounds error for some reason
It seems like n >= animals.length
Check boundary values in the for loops again

Strange Bubble sort behaviour

Can anyone explain why this bubble sort function doesn't work and why I lose numbers in my output? I'm very new to C, so please forgive me if this is something very obvious I have missed.
#include <stdio.h>
#include <stdlib.h>
int bubble(int array[],int length) {
int i, j;
int temp;
for(i = 0; i < (length); ++i) {
for(j = 0; j < (length - 1); ++j) {
if(array[i] > array[i+1]) {
temp = array[i+1];
array[i+1] = array[i];
array[i] = temp;
}
}
}
return 0;
}
int main() {
int array[] = {12,234,3452,5643,0};
int i;
int length;
length = (sizeof(array)/sizeof(int));
printf("Size of array = %d\n", length);
bubble(array, length);
for (i = 0; i < (length); ++i) {
printf("%d\n", array[i]);
}
return 0;
}
Output
Size of array = 5
12
234
3452
0
0
In your inner loop, you don't use j at all. Double check your logic.
Also note that array[i+1] goes beyond the array boundary.
for (i = 0; i < (length-1); ++i) {
for (j = 0; j < (length-i-1); ++j) {
if(array[j] > array[j+1]) {
temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
In a bubble sort you only use the inner loop variable.
Another thing, the inner loop goes from 0 to i if I remember well; but I think that's just an optimisation (as the tail is remains sorted in each step).
Try to run step by step your code with paper and pencil. That always works.
for (i = 0; i < (length); i++) {
for (j = 1; j < (length-i); j++) {
if(array[j-1] > array[j]) {
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
}
}

Resources