0

Does anybody have any idea that why the output is not the sorted array and how should edit the code and why the output is trash

#include <iostream> using namespace std; void swap(int *,int *); void sort (int [],int ); int main() { const int size=12; int a[size]={1,2,96,21,17,3,9,14}; sort(a,size); for(int i=0;i<size;i++) cout<<a[i]<<" ";} void sort (int a[],int size) { int i,j; int *p; p=a; for(i=0;i<size-1;i++) for(j=1;j<size;j++) if (*(p+j+1)<*(p+j)) swap ((p+j+1),(p+j));} void swap(int *pa,int *pb) {int t; t=*pa; *pa=*pb; *pb=t; }

4th Jul 2021, 10:20 AM
Nariman Tajari
Nariman Tajari - avatar
3 odpowiedzi
+ 2
Jayakrishna🇮🇳 Still it is not sorted
4th Jul 2021, 3:56 PM
Viraj
Viraj - avatar
0
/* Corrected code : Your size=12 is more than array size so counting garbage values. In loop, 1st loop condition is should be I<size, next inner loop should be I<size-1 */ #include<iostream> using namespace std; void swap(int *,int *); void sort(int [],int); int main() { int a[]={1,2,96,21,17,3,9,14}; const int size=8; sort(a,size); for(int i=0;i<size;i++) cout<<a[i]<<" "; } void sort (int a[],int size) { int i,j; int *p; p=a; for(i=0;i<size;i++) for(j=1;j<size-1;j++) if (*(p+j+1)<*(p+j)) swap ((p+j+1),(p+j)); } void swap(int *pa,int *pb) { int t; t=*pa; *pa=*pb; *pb=t; }
4th Jul 2021, 10:55 AM
Jayakrishna 🇮🇳
0
Guru patel I am getting correct order of sorting for this program. What order you getting wrong?
5th Jul 2021, 12:43 PM
Jayakrishna 🇮🇳