function to sort array  - Page 1
Forum section: electronics / general

author: Kittu20
author_rank: Regular Contributor
timestamp: January 18, 2023, 01:17:19 pm
content: Hi,I have written code to sort array numbers in increasing orderCode:#include <stdio.h>int main(){int i = 0;int j = 0;int temp;int array = { 50, 40, 30, 20, 10};for ( i = 0; i < 5; i++){for ( j = i + 1; j < 5; j++){temp = array;array = array;array = temp;}}for ( i = 0; i < 5; i++){printf(" %d ", array);}return 0;}Program outputCode:10 20 30 40 50I am now trying to make a function to sort array in increasing orderCode:#include <stdio.h>void foo ( int *ptr){// How to sort array in increasing order in function}int main(){int i = 0;int j = 0;int temp;int array = { 50, 40, 30, 20, 10};foo(&array);// print array after calling functionfor ( i = 0; i < 5; i++){printf(" %d ", array);}return 0;}I am looking help to make function so how to make function that sort array in increasing order

author: DiTBho
author_rank: Super Contributor
timestamp: January 18, 2023, 02:01:40 pm
content: I think the simplest sorting algorithm is "Bubble Sort".It works by repeatedly swapping the adjacent elements if they are in the wrong order.

author: Kittu20
author_rank: Regular Contributor
timestamp: January 18, 2023, 05:30:12 pm
content: Quote from: DiTBho on January 18, 2023, 02:01:40 pmI just want to do in a function what i did in main function. I can't figure out how to do it

author: DiTBho
author_rank: Super Contributor
timestamp: January 18, 2023, 06:10:22 pm
content: Code:void data_sort(uint32_t my_data[],uint32_t my_size){uint32_t i0;uint32_t i1;uint32_t val0;uint32_t val1;for (i0 = 0; i0 < my_size; i0++){for (i1 = 0; i1 < my_size; i1++){val0 = my_data;val1 = my_data;if (val0 <= val1){my_data = val1;my_data = val0;}}}}void data_show(uint32_t my_data[],uint32_t my_size,uint32_t width){uint32_t i0;printf("{\n");for (i0 = 0; i0 < my_size; i0++){if (((i0) remaind width)isEqualTo 0){printf(" ");}printf("%05lu ", my_data);if (((i0 + 1) remaind width)isEqualTo 0){printf("\n");}}if ((i0 remaind width)isNotEqualTo 0){printf("\n");}printf("}\n");}int main(){uint32_t my_data[] ={50, 40, 30, 20, 10,150, 340, 130, 2, 1,250, 440, 530, 9, 11,};uint32_t my_size = (sizeof(my_data) / sizeof(uint32_t));data_sort(my_data, my_size);data_show(my_data, my_size, 4);return 0;}Code:{00001 00002 00009 0001000011 00020 00030 0004000050 00130 00150 0025000340 00440 00530}Something like this. Mind, there are things to be fixed and modified.(the two for-loops can be optimized: your homework).This source is similar to C, adapt it to C89.Have fun

author: TheCalligrapher
author_rank: Regular Contributor
timestamp: January 18, 2023, 06:55:45 pm
content: Quote from: Kittu20 on January 18, 2023, 01:17:19 pmNo, you haven't. There's nothing in your code to impose any kind of specific order (increasing, decreasing or whatever) onto the array. Your code is simply doing a fancy shuffle.Quote from: Kittu20 on January 18, 2023, 01:17:19 pmNo, this is just a coincidence. Take a different array and try again. Your code does not sort the array.

author: mwb1100
author_rank: Frequent Contributor
timestamp: January 18, 2023, 07:53:14 pm
content: Quote from: TheCalligrapher on January 18, 2023, 06:55:45 pmMore specifically, it is reversing the array.

author: SiliconWizard
author_rank: Super Contributor
timestamp: January 18, 2023, 08:49:56 pm
content: All those threads are absolutely not a succession of homework, are they?

author: TheCalligrapher
author_rank: Regular Contributor
timestamp: January 18, 2023, 08:55:23 pm
content: Quote from: mwb1100 on January 18, 2023, 07:53:14 pmTrue, but if reversing were the OP's intent, they would probably opt to implement it in a much less convoluted fashion.This looks like a shot at selection sort. But they forgot to turn it into a sort.

author: IanB
author_rank: Super Contributor
timestamp: January 18, 2023, 08:56:11 pm
content: Quote from: SiliconWizard on January 18, 2023, 08:49:56 pmAlso, this thread seems to be heading in the direction of pointers, which is the rock on which many students will stumble.

author: t3
author_rank: Newbie
timestamp: January 18, 2023, 09:28:06 pm
content: https://en.wikipedia.org/wiki/Sorting_algorithmhttps://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffleI highly recommend finding all plain-text descriptions of different algorithms that you can and implementing them. Writing a shuffle function will be useful for testing.

author: SiliconWizard
author_rank: Super Contributor
timestamp: January 18, 2023, 09:30:41 pm
content: Quote from: TheCalligrapher on January 18, 2023, 08:55:23 pmYes, it misses the condition on order.But this thing is still O(n^2). Not very efficient for reversing an array, is it?

author: Kittu20
author_rank: Regular Contributor
timestamp: January 18, 2023, 09:38:41 pm
content: Quote from: SiliconWizard on January 18, 2023, 08:49:56 pmQuote from: IanB on January 18, 2023, 08:56:11 pmI sorted the array first in main function, I thought it would be nice to make a function. I use pointer to alter array from function. so now i don't understand how to complete the function.

author: gf
author_rank: Super Contributor
timestamp: January 18, 2023, 10:08:47 pm
content: You can also use the functionqsort()from the C library.

author: MIS42N
author_rank: Frequent Contributor
timestamp: January 22, 2023, 01:42:43 am
content: Quote from: Kittu20 on January 18, 2023, 09:38:41 pmAre you sure you have read the replies? DiTBho wrote the function for you. You have to pass two pieces of information - what array is to be sorted, and how many elements.Also, although you say you sorted the array, it was an accident. You didn't compare adjacent numbers to see if they needed swapping. It just happens the array you started with, and the method you use, always needs the elements to be swapped. Put the numbers at the start in any other order, they will not end up sorted. Again, look at DiTBho code which does have the compare.

author: DiTBho
author_rank: Super Contributor
timestamp: January 22, 2023, 12:57:10 pm
content: Code:void data_back(uint32_t my_data[],uint32_t my_size){uint32_t i0;uint32_t i1;uint32_t val0;uint32_t val1;i1 = (my_size - 1);for (i0 = 0; i0 < ((my_size + 1) / 2); i0++){val0 = my_data;val1 = my_data;my_data = val1;my_data = val0;i1--;}}Code:uint32_t my_data[] ={'h', 'A', 'l', 'l', 'o',};uint32_t my_size = (sizeof(my_data) / sizeof(uint32_t));Code:{h A l l o}{o l l A h}reverse order

author: djsb
author_rank: Super Contributor
timestamp: January 22, 2023, 01:17:42 pm
content: Why does nobody include comments in their code any more? It REALLY helps people like me who are trying to follow along. It also helps as a reminder when returning to the code later. Thanks.

author: magic
author_rank: Super Contributor
timestamp: January 22, 2023, 01:42:37 pm
content: First of all, DiTBho should have used i and j as indices instead of i0 and i1Secondly, his sorting algorithm is inefficient and ridiculous (perhaps deliberately so to troll the OP) and I'm not sure if it could be easily explained with a few lines of comment. Maybe the author could prove me wrong

author: DiTBho
author_rank: Super Contributor
timestamp: January 22, 2023, 04:44:15 pm
content: Quote from: magic on January 22, 2023, 01:42:37 pmI always index my loop-indicesSeveral reasons, include 'cause it makes easier to search among the code, even on a poor note-pad.e.g.Search within block of text for "i": what? pr*I*nt?Search within block ot text for "i0": better, no collisions, rights to the goal.Isn't this good? Sorry, but it's good for meQuote from: magic on January 22, 2023, 01:42:37 pmUmmm, I wrote it that way 'cause it's the most intuitive, but I clearly said in my post that it's the worst implementation ever, indeed I concluded my post asking the OP to "re-think" the algorithm loops to make it more efficient.You can do it, and post it.

author: DiTBho
author_rank: Super Contributor
timestamp: January 22, 2023, 04:45:09 pm
content: Quote from: DiTBho on January 18, 2023, 06:10:22 pm

author: magic
author_rank: Super Contributor
timestamp: January 22, 2023, 05:20:40 pm
content: Quote from: DiTBho on January 22, 2023, 04:44:15 pmThat's your punishment for not using vimAs for the algorithm, I recognize it now. It think it's this recently popularized "I don't understand Insertion Sort" algorithm which we have discussed previously on the forum.https://www.eevblog.com/forum/programming/interesting-new-sort-algorithm/msg4316548/#msg4316548

author: IanB
author_rank: Super Contributor
timestamp: January 22, 2023, 06:31:19 pm
content: Quote from: djsb on January 22, 2023, 01:17:42 pmComments can make complex algorithms easier to understand. However, comments can also make the code "noisy" when the code itself is simple enough to understand by itself. There's a balance to be found. It also helps to follow standard patterns so that people can recognize what they see at a glance. Something like this, perhaps?Code:/* Reverse.c */#include <stdio.h>void reverse(int a[], int size);void print(int a[], int size);int main(){int data[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 4 };int datalen = sizeof(data) / sizeof(data);print(data, datalen);reverse(data, datalen);print(data, datalen);return 0;}void reverse(int a[], int size){/* reverses an array of integers in place */int i, j, tmp;for (i = 0, j = size - 1; i < j; ++i, --j) {tmp = a;a = a;a = tmp;}}void print(int a[], int size){/* prints a list of integers */int i;for (i = 0; i < size; ++i) {printf("%d ", a);}printf("\n");}Output:Code:3 1 4 1 5 9 2 6 5 44 5 6 2 9 5 1 4 1 3

author: brucehoult
author_rank: Super Contributor
timestamp: January 22, 2023, 08:55:17 pm
content: Quote from: djsb on January 22, 2023, 01:17:42 pmThe very mostest fun is when the comments are wrong. e.g. "// sort the array" when you're actually reversing it.

author: SiliconWizard
author_rank: Super Contributor
timestamp: January 22, 2023, 09:02:07 pm
content: Beyond the point about comments, that has been discussed in another thread lately already (so please let's not start it all over again here), it was rather likely that this thread was some homework, and people are already nice to give code snippets (I don't think they should) rather than just general pointers. So expecting them to comment their code snippets is frankly absurd IMHO.

author: DiTBho
author_rank: Super Contributor
timestamp: January 22, 2023, 09:15:50 pm
content: Quote from: magic on January 22, 2023, 05:20:40 pmYeah, I remember people in this forum said it's the most intuitive, so I... I used that as an example. But, behing my decision, I thought that, despite it's good because it "just works", one can immediately recognize that it sucks here and there, so so it's also offers a good goal for a beginner: to make it better

author: magic
author_rank: Super Contributor
timestamp: January 22, 2023, 09:37:46 pm
content: LOL, I was actually the one who I think argued the loudest that it is a trivial derivative of a known trivial algorithmBut I wouldn't exactly describe it as intuitive, because there is no sensible concept to it that I can see. Bubble Sort repeatedly scans the array and pushes the largest element to the end. Insertion Sort picks each element in turn and moves it backwards as far as it fits. The only way I can explain Idiot Sort is that it performs Insertion Sort as a subset of its operations and then the remaining operations don't screw up the work done by this Insertion Sort subset.For beginners it is probably better to first learn algorithms that are designed to work in accordance with a clear principle of operation, then try to analyze random junk like Idiot Sort