Reverse an array upto a given position only - arrays

How to reverse an array upto specified position in c#

static int[] ReverseArray(int[] arr, int i) {
int[] arrnew = new int[8];
int k = i;
for (int j = 0; j < k / 2; j++)
{
int temp = arr[j];
arr[j] = arr[k - j - 1];
arr[k - j - 1] = temp;
}
return arr;
}

Related

Why aren't my for loops working in this bubble sort function in C

I was having this problem to which I have come to the solution through the trial and error process but I have no idea why my bubble sort function wasn't working in the first place.
The problem had to do with the for-loops inside my function. Specifically when declaring and defining my i and j variables.
In my version of C I can define variables inside my for-loop parameter, but I can't declare them, so I do both the declaration and definition outside.
Doing so though made my function not work as intended as it didn't sort my array at all.
Though after declaring the variables outside but defining them inside the for-loop parameter to my surprise the function worked properly. My problem is I have no idea why.
Here I am providing both the working version and the non-working version:
Non-Working Version:
void bubbleDesc (int n, int array[])
{
int i = 0, j = 0, temp;
for (i; i < n - 1; i++)
{
for (j; j < n - 1; j++)
{
if (array[j] < array[j + 1])
{
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
}
Working Version:
void bubbleDesc (int n, int array[])
{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1; j++)
{
if (array[j] < array[j + 1])
{
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
}
Your not working implementation misses the initialization of j. So it iterates the inner loop only in the first iteration of the outer loop.
void bubbleDesc (int n, int array[])
{
int i = 0, j = 0, temp;
for (i; i < n - 1; i++)
{
j = 0; // this init is needed
for (j; j < n - 1; j++)
{
if (array[j] < array[j + 1])
{
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
}
Thought, better would be to limit the scope of j and declare it only in the block where it is used, i.e., for (int j=0;...){...}
For starters it is better to specify in the function declaration the array as its first parameter and the number of elements in the array as its second parameter.
Also in C sizes of entities are defined as values of the type size_t.
So the function should be declared like
void bubbleDesc( int array[], size_t n );
In your first function implementation the variable j used in the inner for loop is initialized only once before the for loops
void bubbleDesc (int n, int array[])
{
int i = 0, j = 0, temp;
for (i; i < n - 1; i++)
{
for (j; j < n - 1; j++)
//...
though you need to initialize it to 0 each time before the inner for loop will get the control. The second function implementation does such an initialization within the for loop statement.
void bubbleDesc (int n, int array[])
{
int i, j, temp;
for ( i = 0; i < n - 1; i++ )
{
for ( j = 0; j < n - 1; j++ )
//...
Bear in mind that you should declare variables in minimum scopes where they are used. That is it will be better at least to write
void bubbleDesc (int n, int array[])
{
for ( int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1; j++)
{
if (array[j] < array[j + 1])
{
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
}
Or if your compiler does not allow to declare variables in the for loop statement then you can write
void bubbleDesc (int n, int array[])
{
int i = 0;
for ( ; i < n - 1; i++)
{
int j = 0;
for ( ; j < n - 1; j++)
{
if (array[j] < array[j + 1])
{
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
But in any case your implementation of the function is not efficient because even when the whole array or its second part is already sorted nevertheless the loops continue its iterations for all elements of the array.
Another drawback is that if you will want to sort the array in the ascending order you will have to write a new function.
You should always try to write more general functions.
It will be better to add to the function a third parameter that will specify a comparison function that will compare elements of array.
Here is a demonstration program that shows how the function can be written.
#include <stdlib.h>
void bubbleSort( int a[], size_t n, int cmp( const void *, const void * ) )
{
for (size_t last = 1; !( n < 2 ); n = last)
{
for (size_t j = last = 1; j < n; j++)
{
if (cmp( a + j, a + j - 1 ) < 0)
{
int tmp = a[j];
a[j] = a[j - 1];
a[j - 1] = tmp;
last = j;
}
}
}
}
static int ascending( const void *a, const void *b )
{
int x = *( const int * )a;
int y = *( const int * )b;
return ( y < x ) - ( x < y );
}
static int descending( const void *a, const void *b )
{
int x = *( const int * )a;
int y = *( const int * )b;
return ( x < y ) - ( y < x );
}
int main( void )
{
int a[] = { 9, 2, 6, 5, 8, 1, 3, 7, 4, 0 };
const size_t N = sizeof( a ) / sizeof( *a );
for (size_t i = 0; i < N; i++)
{
printf( "%d ", a[i] );
}
putchar( '\n' );
bubbleSort( a, N, ascending );
for (size_t i = 0; i < N; i++)
{
printf( "%d ", a[i] );
}
putchar( '\n' );
bubbleSort( a, N, descending );
for (size_t i = 0; i < N; i++)
{
printf( "%d ", a[i] );
}
putchar( '\n' );
}
The program output is
9 2 6 5 8 1 3 7 4 0
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0

Bubble sort in C

I was trying to implement bubble sort algorithm to sort a 10 element array. I've tried writing the code below and it doesn't seem wrong, but it doesn't sort the elements at all.
Could someone give me a hand?
This is the code:
`
#include <stdio.h>
#define DIM 10
int main() {
int arr[DIM] = {1, 5, 6, 8, 7, 9, 3, 2, 4, 10};
int tmp;
puts("Original array: ");
for (int i = 0; i < DIM; i++) {
printf("%3d", arr[i]);
}
// Bubble sort
for (int i = 0; i < DIM - 1; ++i) {
for (int j = 0; j < DIM - i - 1; ++j) {
// Compare two elements and swap if first > second
// Use of tmp variable (temporary)
if (arr[i] > arr[i + 1]) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
}
}
puts("");
puts("Ascending order arrray: ");
for (int i = 0; i < DIM; i++) {
printf("%3d", arr[i]);
}
puts("");
}
`
In the second loop, j should be used instead of i
if (arr[j] > arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
Use j in the body of the second loop instead of i.`
// Bubble sort
for (int i = 0; i < DIM - 1; ++i) {
for (int j = 0; j < DIM - i - 1; ++j) {
// Compare two elements and swap if first > second
// Use of tmp variable (temporary)
if (arr[j] > arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
#include <stdio.h>
#define DIM 10
int main() {
int arr[DIM] = {1, 5, 6, 8, 7, 9, 3, 2, 4, 10};
int tmp;
puts("Original array: ");
for (int i = 0; i < DIM; i++) {
printf("%3d", arr[i]);
}
// Bubble sort
for (int i = 0; i < DIM - 1; ++i) {
for (int j = 0; j < DIM - i - 1; ++j) {
// Compare two elements and swap if first > second
// Use of tmp variable (temporary)
if (arr[j] > arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
puts("");
puts("Ascending order arrray: ");
for (int i = 0; i < DIM; i++) {
printf("%d", arr[i]);
}
puts("");
}
*Change: need to use "j" inplace of "i" in the 2nd for loop statements.

Stack around Variable a is corrupted

#include<stdio.h>
int main(void)
{
int array[10] = { 10,2,9,4,5,6,7,8,3,1 };
/*Implementing Bubble Sort */
int temp;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 10 - i; j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
for (int i = 0; i < 10; i++)
{
printf("%d ", array[i]);
}
}
When I try to run the program I'm getting values sorted but one value has some garbage value and the dialogue box appears that stack around variable is corrupted in VS 2019. In some other compiler I'm getting 0 in place of 10 in compiler.
The inner for .loop
for (int j = 0; j < 10 - i; j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
invokes undefined behavior because when j is equal to 9 for the first iteration of the outer loop that is when i is equal to 0 the index in the expression array[j + 1] can be equal to 10 that results in accessing the memory beyond the array.
Rewrite the loop like
for (int j = 1; j < 10 - i; j++)
{
if (array[j-1] > array[j])
{
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
}
You can you this logic slightly edited from yours.
#include<stdio.h>
int main(void)
{
int array[10] = { 10,2,9,4,5,6,7,8,3,1 };
/*Implementing Bubble Sort */
int temp;
for (int i = 0; i < 10; i++)
{
for (int j = i; j < 10; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for (int i = 0; i < 10; i++)
{
printf("%d ", array[i]);
}
}

Sort main diagonal of array by Shaker sort

So that's my task. I don't know how to realize it in 2D.
All variables (i, j, L, R, etc. are integer)
while (L < R)
{
for(i, j = L; i < R; i++, j++)
{
if (a[i][j] > a[i + 1][j + 1])
{
temp = a[i][j];
a[i][j] = a[i + 1][j + 1];
a[i + 1][j + 1] = temp;
k = i;
}
}
R = k;
for(i, j = R - 1; i >= L; i--, j--)
{
if(a[i][j] > a[i + 1][j + 1])
{
temp = a[i][j];
a[i][j] = a[i + 1][j + 1];
a[i + 1][j + 1] = temp;
k = i;
}
}
L = k + 1;
}
I tried this code but I think that something is wrong with that.
Input:
1 10 5
4 0 8
8 18 3
Output: (That should be)
0 10 5
4 1 8
8 18 3
But current Output is
1 10 5
4 0 8
8 18 3
A function to swap values using call by reference.
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
A function implementing shaker sort
void ShakerSort(int a[], int n)
{
int i, j, k;
for(i = 0; i < n;)
{
// First phase for ascending highest value to the highest unsorted index.
for(j = i+1; j < n; j++)
{
if(a[j] < a[j-1])
swap(&a[j], &a[j-1]);
}
// Decrementing highest index.
n--;
// Second phase for descending lowest value to the lowest unsorted index.
for(k = n-1; k > i; k--)
{
if(a[k] < a[k-1])
swap(&a[k], &a[k-1]);
}
// Incrementing lowest index.
i++;
}
}
The main function looks like this
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
ShakerSort(arr, n);
// Printing the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}
The above code is written in c++ refer it and make the changes according to your requirment

In C: negative numbers insertion sort

I'm fairly new into programming and my teacher want me to implement insertion sort in C.
My code works, but not with negative numbers if I use it with negative numbers in my array I always get a segmentation fault:
void insertion_sort(int array[], int len) {
int i = 0;
int j = 1;
signed int tmp = array[0];
for(i = 1; i < len; i++) {
tmp = array[i];
j = i - 1;
if(j >= 0){
while(tmp < array[j]) {
array[j + 1] = array[j];
j = j - 1;
}
}
array[j + 1] = tmp;
}
}
Change this code snippet
if(j >= 0){
while(tmp < array[j]) {
array[j + 1] = array[j];
j = j - 1;
}
}
to
while ( j >= 0 && tmp < array[j] ) {
array[j + 1] = array[j];
j = j - 1;
}
Take into account that there is no sense to initialize defined variables
int i = 0;
int j = 1;
signed int tmp = array[0];
because they are overwritten in the followed statements.

Resources