Im in basic C no advanced methods thanjks in advance for you
Im in basic C++ no advanced methods thanjks in advance for your help
write two programs, one that uses a simple bubble sort and one that uses a simple selection sort (do NOT use any of the variations) on a list of integers between 0 and 100. Note: recursion is NOT required for these exercises. See above for the due date.
The list cannot contain more than 100 entries. The bubble sort program should sort the integers in ascending order; the selection sort should sort the integers is ascending order.
Prompt the user for the name of a file containing an unsorted list of integers. The file might contain (without the commas):
100, 94, 59, 83, 7, 11, 92, 76, 37, 89, 74, 59, 65, 79, 49, 89, 89, 75, 64, 82, 15, 74, 82, 68, 92, 61, 33, 95, 91, 82, 89, 64, 43, 93, 86, 65, 72, 40, 42, 90, 81, 62, 90, 89, 35, 81, 48, 33, 94, 81, 76, 86, 67, 70, 100, 80, 83, 78, 96, 58
Use an array of integers to store the data. Place the sorted data in an output file with no more than 10 integers per line in orderly columns.
Before exiting inform the user of the name of the output file, the number of integers in the input file, and the total number of swaps that were required to sort the file.
Solution
Code for Selection sort:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <climits>
#include <stdio.h>
using namespace std;
int swapSelection=0;
int swapBubble=0;
void swap(int *a,int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
return;
}
void selectionSort(int array[100],int n)
{
cout<<\"Elements:\"<<n<<endl;
const char *filename = \"fileSelection.txt\";
FILE *output = fopen( filename, \"w\" );
int min=INT_MAX;
for(int i=0;i<n;i++){
min=i;
for(int j=i+1;j<n;j++)
{
if(array[j]<array[min])
min=j;
}
if(min!=i)
{
swap(&array[min],&array[i]);
swapSelection++;
}
}
int k=0;
/*for(int i=0;i<n;i++)
{
cout<<array[i]<<endl;
}*/
//fprintf(output,\"%d\\t\",array[0]);
while(k<n )
{
fprintf(output,\"%d\\t\",array[k]);
if((k+1)%10==0 ) fprintf(output, \"\ \" );
k++;
}
fclose( output);
cout<<\"Filename:\"<<filename<<endl;
cout<<\"Number of integers:\"<<n<<endl;
cout<<\"Number of swaps:\"<<swapSelection<<endl;
}
void bubbleSort(int array[100])
{
}
int main() {
const char *delim = \" \";
const char *filename = \"file.txt\";
FILE *input = fopen( filename, \"r\" );
char buffer[ 1024 ];
char *tok;
int array[100],i=0;
if( input == NULL ){
fprintf( stderr, \"File Not opened!!!!\ \");
}else{
while( fgets(buffer, 1024, input) != NULL ){
tok = strtok( buffer, delim );
while( tok != NULL ){
//printf( \"%s\ \", tok );
int x=atoi(tok);
array[i++]=x;
tok = strtok( NULL, delim );
}
}}
if( ferror(input) ){
perror( \"Error:\" );
}
fclose( input);
selectionSort(array,i-1);
return 0;
}
OUTPUT:
7 11 15 33 33 35 37 40 42 43
48 49 58 59 59 61 62 64 64 65
65 67 68 70 72 74 74 75 76 76
78 79 80 81 81 81 82 82 82 83
83 86 86 89 89 89 89 89 90 90
91 92 92 93 94 94 95 96 100
Filename:fileSelection.txt
Number of integers:59
Number of swaps:55
Code for Bubble sort:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <climits>
#include <stdio.h>
using namespace std;
int swapBubble=0;
void swap(int *a,int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
return;
}
void bubbleSort(int array[100],int n)
{
cout<<\"Elements:\"<<n<<endl;
const char *filename = \"fileBubble.txt\";
FILE *output = fopen( filename, \"w\" );
bool swapped=false;
for(int i=0;i<n;i++)
{
swapped=false;
for(int j=0;j<n;j++){
if(array[j]>array[j+1])
{
swap(&array[j],&array[j+1]);
swapped=true;
swapBubble++;
}
}
if(!swapped)
break;
}
int k=0;
while(k<n )
{
fprintf(output,\"%d\\t\",array[k]);
if((k+1)%10==0 ) fprintf(output, \"\ \" );
k++;
}
fclose( output);
cout<<\"Filename:\"<<filename<<endl;
cout<<\"Number of integers:\"<<n<<endl;
cout<<\"Number of swaps:\"<<swapBubble<<endl;
}
int main() {
const char *delim = \" \";
const char *filename = \"file.txt\";
FILE *input = fopen( filename, \"r\" );
char buffer[ 1024 ];
char *tok;
int array[100],i=0;
if( input == NULL ){
fprintf( stderr, \"File Not opened!!!!\ \");
}else{
while( fgets(buffer, 1024, input) != NULL ){
tok = strtok( buffer, delim );
while( tok != NULL ){
printf( \"%s\ \", tok );
int x=atoi(tok);
array[i++]=x;
tok = strtok( NULL, delim );
}
}}
if( ferror(input) ){
perror( \"Error:\" );
}
fclose( input);
bubbleSort(array,i-1);
return 0;
}
OUTPUT:
7 11 15 33 33 35 37 40 42 43
48 49 58 59 59 61 62 64 64 65
65 67 68 70 72 74 74 75 76 76
78 79 80 81 81 81 82 82 82 83
83 86 86 89 89 89 89 89 90 90
91 92 92 93 94 94 95 96 100
Filename:fileBubble.txt
Number of integers:59
Number of swaps:834



