How do I make it so after the first pass, the largest number is guaranteed to be in the highest-numbered element of the array; after the second pass, the two highest numbers are “in place,” and so on.
The current code only makes it pass from largest to smallest but I do not want that.
I'm only trying to do this for the data items in original order.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
int main(void)
{
int a[SIZE] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
int noc = 0;
puts("Data items in original order");
for (int noc = 0; noc < 9; noc++) {
printf("\n");
for (size_t i = 0; i + noc < SIZE; i++) {
printf("%4d", a[i]);
}
for (int pass = 1; pass < SIZE; pass++) {
for (size_t i = 0; i < SIZE - pass; i++) {
if (a[i] > a[i - 1]) {
int hold = a[i];
a[i] = a[i - 1];
a[i - 1] = hold;
}
}
}
}
for (int pass = 1; pass < SIZE; pass++) {
for (size_t i = 0; i < SIZE - pass; i++) {
if (a[i] > a[i + 1]) {
int hold = a[i];
a[i] = a[i + 1];
a[i + 1] = hold;
}
}
}
puts("\nData items in ascending order");
for (size_t i = 0; i + noc < SIZE; i++) {
printf("%4d", a[i]);
}
puts("");
}
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
int main(void)
{
int a[SIZE] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
int i;
int pass = 0;
while(pass < SIZE - 1){
int index = SIZE - pass - 1;
int MAX = a[index];
for(i=0;i<SIZE - pass;i++){
if(a[i]>MAX) {
MAX = a[i];
index = i;
}
}
a[index] = a[SIZE - pass - 1];
a[SIZE - pass - 1] = MAX;
for(i=0 ;i < SIZE ; i++ ) printf("%d ",a[i]);
printf("\n");
pass++;
}
}
You can use several sort algorithm for that
shellsort
bubblesort ( slowest alg )
heapsort
insertion sort
tree sort
QuickSort
There are a lot of sort algo that placed the integer asc or desc
Related
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.
Why is it so that in insertion sort when the cases are (n-1) then also if we write for loop for i=0 then we have to consider n cases but in bubble sort where we have to consider (n-1) cases again then we don't face any such problem? I am not getting this thing please help me in this.
#include <stdio.h>
void printArray(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
}
void insertionSort(int *a, int n)
{
int key, j;
for (int i = 0; i < n; i++) // for number of passes
{
printf("\nPass number %d started.", i);
key = a[i];
j = i - 1;
while (j >= 0 && a[j] > key)
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
}
int main()
{
int arr[] = {12, 54, 65, 7, 23, 9};
int len = 6;
printf("Array before insertion sorting:\n");
printArray(arr, len);
insertionSort(arr, len);
printf("\nArray after insertion sorting:\n");
printArray(arr, len);
return 0;
}
and when we write for loop for i =1, then it takes (n-1) cases. How can the number of cases change with a change in 'i' in the for loop. Shouldn't it be like for(int i = 0;i<n;i++);???
#include <stdio.h>
void printArray(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
}
void insertionSort(int *a, int n)
{
int key, j;
for (int i = 1; i <= n - 1; i++) // for number of passes
{
printf("\nPass number %d started.", i);
key = a[i];
j = i - 1;
while (j >= 0 && a[j] > key)
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
}
int main()
{
int arr[] = {12, 54, 65, 7, 23, 9};
int len = 6;
printf("Array before insertion sorting:\n");
printArray(arr, len);
insertionSort(arr, len);
printf("\nArray after insertion sorting:\n");
printArray(arr, len);
return 0;
}
Thanks for your answers. I got it guys in insertion sort we don't compare index 0 element with anything and that's the reason we have to start it from i =1 whereas in bubble sort we have to compare every element even the element at index 0.
I am trying to implement my first sorting algorithm in C, where an array of numbers is given as a command-line argument before a function is called to sort the array and print the output. The program re-prints the array before sorting it, so as far as I can tell the error is in the sorting algorithm itself. Here is the function:
void ascending(int n, int arr[])
{
for (int i = 0; i < (n - 1); i++)
{
//min is equal to i (use as index)
int min = i;
//Compare arr[i] to all other elements in the array
for (int j = i + 1; j < n; j++)
{
//If a smaller number is found, its index (j) is now min
if (arr[j] < arr[min])
{
min = j;
//Swapping values to be in correct place
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
}
printf("sorted: ");
for (int i = 0; i < n; i++)
{
printf("%i, ", arr[i]);
}
printf("\n");
}
If I ask it to sort [5, 4, 60, 2, 1] it will correctly sort the output to sorted: [1, 2, 4, 5, 60]
But if I ask it to sort [60, 5, 3, 3, 1, 4, 2] it will print: [2, 1, 4, 3, 3, 5, 60], sorting some numbers but not others.
Thanks in advance!
In Selection Sort, the goal of the inner for loop is to find the index of the minimum element in the un-sorted subarray (i.e., the array starting at index i and ending at index n-1). The swap should occur after you have found the min value, so that it is correctly placed at index i in the array:
void ascending(int n, int arr[])
{
for (int i = 0; i < (n - 1); i++)
{
//min is equal to i (use as index)
int min = i;
//Compare arr[i] to all other elements in the array
for (int j = i + 1; j < n; j++)
{
//If a smaller number is found, its index (j) is now min
if (arr[j] < arr[min])
{
min = j;
}
}
//Swapping values to be in correct place
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
printf("sorted: ");
for (int i = 0; i < n; i++)
{
printf("%i, ", arr[i]);
}
printf("\n");
}
In this if statement
if (arr[j] < arr[min])
{
min = j;
//Swapping values to be in correct place
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
after the assignment
min = j;
this swapping
//Swapping values to be in correct place
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
does not make a sense.
The inner for loop can be written the following way
for (int j = i + 1; j < n; j++)
{
//If a smaller number is found, its index (j) is now min
if (arr[j] < arr[min])
{
min = j;
}
}
if ( min != i )
{
//Swapping values to be in correct place
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
Pay attention to that it is better to declare the function the way when the first parameter specifies the array and the second parameter specifies the number elements in the array,
For example
void ascending( int arr[], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
size_t min = i;
for ( size_t j = i + 1; j < n; j++ )
{
if ( arr[j] < arr[min] ) min = j;
}
if ( min != i )
{
int tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
}
}
}
I am trying to create a reverse bubble sort algorithm. It works but the first output is a big crazy number that i dont understand. The remaining outputs seem to be sorted in descending order. Where is my code wrong?
#include <stdio.h>
void ft_rev_int_tab(int *tab, int size)
{
int i;
int j;
int k;
i = 0;
while (i < size)
{
j = 0;
while (j < size -i)
{
if (tab[j] < tab[j+1])
{
k = tab[j];
tab[j] = tab[j+1];
tab[j + 1] = k;
}
j++;
}
i++;
}
}
int main(void)
{
int tab[] = {9, 320, 0, 113, 15};
int size = sizeof(tab) / sizeof(*tab);
ft_rev_int_tab(tab, size);
for (int i = 0; i < size; i++)
{
printf ("%d\n", tab[i]);
}
}
i = 0; before while (i < size) is wrong. The condition j < size -i is equivalent to j < size when i is zero. In this case j will be at most size-1 and now tab[j+1] is out-of-range.
It should be changed to i = 1;.
void ft_rev_int_tab(int *tab, int size) {
int i;
int j;
int k;
i = 0;
while (i < size) {
j = 0;
while (j < size - i - 1) {
if (tab[j] < tab[j + 1]) {
k = tab[j];
tab[j] = tab[j + 1];
tab[j + 1] = k;
}
j++;
}
i++;
}
}
Your condition in the nested while loop should be (j < size - i - 1)
because you only need to check the value from tab[0] to tab[i-1]. Your if and swap checks tab[j] and tab[j+1], so when i is equal to the size of the array, you will compare it with a value outside of the array. In your example is the tab[5]'s value.
#include <stdio.h>
int size;
void SelectionSort(int a[], int size) {
int i, j, t, min, b[] = { 0,0,0,0,0,0,0,0 };
printf("\nelements to sort : ");
for (t = 0; t < size; t++) printf("%d ", a[t]);
printf("\n\n<<<<<<<<<<<<<<<<< selection sort >>>>>>>>>>>>\n");
for (i = 0; i < size - 1; i++) {
min = i;
for (j = i + 1; j < size; j++) {
if (a[j] < a[min]) min = j;
}
b[i] = a[min];
printf("\nA ARRAY %d : ", i + 1);
for (t = 0; t < size; t++) printf("%3d", a[t]);
printf("\nB ARRAY %d : ", i + 1);
for (t = 0; t < size; t++) printf("%3d", b[t]);
}
}
void main() {
int list[8] = { 69, 10, 30, 2, 16, 8, 31, 22 };
size = 8;
SelectionSort(list, size);
getchar();
}
the issue is that whenever the comparing is done the number 2 is copied in the array
what should i do to fix this?
Each step of the selection sort needs to
Identify the least element in the subarray
Remove the element
Place it at the front
Your code is skipping step 2. So it keeps finding the 2 until i increments past it.
Instead of copying into a different array b, swap the lowest element with a[i].
t = a[min];
a[min] = a[i];
a[i] = t;
BTW, it would be helpful in this sort of question to show us the output so potential answerers don't need to compile and run the program themselves.
Here is a modification of the usual selection sort, that takes duplicates into account.
In each inner loop, the whole array is read. Only values larger than the preceding minimum (lmin) are considered, and values equal to the current accepted minimum (min) are counted (nmin is the count). Then, the array b is updated with nmin values equal to min. The first loop is a special case as there is no preceding minimum.
#include <stdio.h>
void selection_sort(int n, int a[n], int b[n]) {
int j, k, lmin, min, nmin, first;
min = a[0];
nmin = 1;
for (j = 1; j < n; j++) {
if (a[j] == min) {
nmin++;
} else if (a[j] < min) {
min = a[j];
nmin = 1;
}
}
for (k = 0; nmin > 0; nmin--) {
b[k++] = min;
}
while (k < n) {
lmin = min;
first = 1;
for (j = 0; j < n; j++) {
if (a[j] > lmin) {
if (first) {
first = 0;
min = a[j];
nmin = 1;
} else if (a[j] == min) {
nmin++;
} else if (a[j] < min) {
min = a[j];
nmin = 1;
}
}
}
for ( ; nmin > 0; nmin--) {
b[k++] = min;
}
}
}
#define N 20
int main(void) {
int a[] = {2, 5, 9, 8, 4, 7, 1, 7, 5, 0, 3, 8, 3, 3, 6, 1, 8, 0, 2, 8};
int b[N];
selection_sort(N, a, b);
for (int i = 0; i < N; i++) {
printf("%d ", a[i]);
}
printf("\n");
for (int i = 0; i < N; i++) {
printf("%d ", b[i]);
}
printf("\n");
return 0;
}
The purpose of the boolean first is to detect the first accepted current minimum (that is, the first value that is larger than lmin).
Another way would be to set min = INT_MAX before the beginning of the inner loop, where INT_MAX is the maximum int value (defined in limits.h): we know lmin < INT_MAX, otherwise we would already have exited the function. Here is this variant:
#include <stdio.h>
#include <limits.h>
void selection_sort(int n, int a[n], int b[n]) {
int j, k, lmin, min, nmin;
min = a[0];
nmin = 1;
for (j = 1; j < n; j++) {
if (a[j] == min) {
nmin++;
} else if (a[j] < min) {
min = a[j];
nmin = 1;
}
}
for (k = 0; nmin > 0; nmin--) {
b[k++] = min;
}
while (k < n) {
lmin = min;
min = INT_MAX;
nmin = 0;
for (j = 0; j < n; j++) {
if (a[j] > lmin) {
if (a[j] < min) {
min = a[j];
nmin = 1;
} else if (a[j] == min) {
nmin++;
}
}
}
for ( ; nmin > 0; nmin--) {
b[k++] = min;
}
}
}
#define N 20
int main(void) {
int a[] = {2, 5, 9, 8, 4, 7, 1, 7, 5, 0, 3, 8, 3, 3, 6, 1, 8, 0, 2, 8};
int b[N];
selection_sort(N, a, b);
for (int i = 0; i < N; i++) {
printf("%d ", a[i]);
}
printf("\n");
for (int i = 0; i < N; i++) {
printf("%d ", b[i]);
}
printf("\n");
return 0;
}
Note that in this version, nmin is set to zero before the inner loop: it doesn't matter if there are values smaller than INT_MAX. But if all remaining values are equal to INT_MAX, we are going to count them with nmin++, so we must start at zero.