Sorting Output Numbers Without an Array

Sorting output numbers without an array

Just reverse the for loop:

for( int i = 100; i >= 7; i--){ //There is no integer lower than 7 that is divisible by 7
if(i%7 == 0){
cout << i << endl;

}
}

how to sort numbers without arrays

Well, you can do something close to this: Sort 4 numbers without array

Let me help you:

int tmp;

if (a > b) { tmp = a; a = b; b = tmp; }
if (a > c) { tmp = a; a = c; c = tmp; }
if (b > c) { tmp = b; b = c; c = tmp; }

System.out.println(a + " " + b + " " + c);

How to sort numbers in an ascending order without using an array?

//#include <conio.h>
#include <stdio.h>
// to save the position of the smallest number

// to get numbers from the user and save them in file1
void getNumbers(char *filename) {
char x[255];
FILE *file;
file = fopen(filename, "w");
if (file == NULL) {
printf("Couldn't open the file\n");
return;
}
printf("Enter numbers: ");
scanf("%[^\n]s", &x);
fprintf(file, "%s", x);
fclose(file);
}

// to find the smallest number and copy it to file3
int findSmallestNo(char *filename1, char *filename3) {
int x, temp = 100000;
FILE *file1, *file3;
file1 = fopen(filename1, "r");
file3 = fopen(filename3, "a");
fscanf(file1, "%d", &temp);
while (fscanf(file1, "%d", &x) == 1) {
if (x < temp) {
temp = x;
}
}
fprintf(file3, "%d ", temp);
fclose(file1);
fclose(file3);
return temp;
}

// to copy numbers from file1 to file2 except the smallest number that we copied
// in the file3

void copyToFile2(char *filename1, char *filename2, int min_value) {
int flag = 0, x;
FILE *file1, *file2;
file1 = fopen(filename1, "r");
file2 = fopen(filename2, "w");
while (fscanf(file1, "%d", &x) == 1) {
if (x != min_value || flag == 1) {
fprintf(file2, "%d ", x);
} else {
flag = 1;
}
}
fclose(file1);
fclose(file2);
}

// to copy from file2 to file1 (it should delete the old data in file1)
void copyToFile(char *filename1, char *filename2) {
int x;
FILE *file1, *file2;
file1 = fopen(filename1, "w");
file2 = fopen(filename2, "r");
rewind(file2);

while (fscanf(file2, "%d", &x) == 1) {
fprintf(file1, "%d ", x);
}
fclose(file1);
fclose(file2);
}

// to print numbers on the screen
void print_file(char *filename) {
int x;
FILE *file;
file = fopen(filename, "r");
printf("\nSorted Numbers: ");

while ((fscanf(file, "%d", &x)) == 1) {
printf("%d ", x);
}
printf("\n");
fclose(file);
}

int getLength(char *filename) {
int nn, counting = 1;
FILE *file;
file = fopen(filename, "r");
rewind(file);
while ((fscanf(file, "%d ", &nn) == 1)) {
counting++;
}
fclose(file);
return counting;
}

void clearFile (char *filename) {
FILE *file;
file = fopen(filename, "w");
fclose(file);
}

int main() {
int len, count = 1;
char *filename1 = "file1.txt";
char *filename2 = "file2.txt";
char *filename3 = "file3.txt";

clearFile(filename3);

// to get the count of numbers in file1
getNumbers(filename1);

len = getLength(filename1);

// while loop until var count is equal to the count of numbers in file1
while (count < len) {
++count;
int cur_min = findSmallestNo(filename1, filename3);
copyToFile2(filename1, filename2, cur_min);
copyToFile(filename1, filename2);
}

int number;
// to copy numbers from file3 to file1
copyToFile(filename1, filename3);
clearFile(filename3);
// to print numbers on the screen in file1
print_file(filename1);
return 0;
}

i fixed your code, it seems working fine.

your algorithm works, but i think like @Bananenkönig said "Copying between files is rather inefficient!".
The problem with your code is just about working with files.

Your way to read till end of file with fscanf will read 1 more time to find the eof, that's why you'll get some unwanted number in your results.

how to input n numbers and print it in ascending order without using array

List<Integer> list = new LinkedList<Integer>();
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.print("Enter a number, -1 to exit: ");
int num = scanner.nextInt();

if (num == -1) break;

list.add(num);
}

// sort the list (ascending)
Collections.sort(list);

// output the list
for (Integer val : list) {
System.out.println(val);
}

python: order a list of numbers without built-in sort, min, max function

I guess you are trying to do something like this:

data_list = [-5, -23, 5, 0, 23, -6, 23, 67]
new_list = []

while data_list:
minimum = data_list[0] # arbitrary number in list
for x in data_list:
if x < minimum:
minimum = x
new_list.append(minimum)
data_list.remove(minimum)

print (new_list)

#Added parenthesis

ascending sort without using arrays in java

Oddly I couldn't really find much on sorting outside of arrays and lists so I will assist you.

Code:

  int temp;

for (int count = 0; count < 4; count++)
{
if ( number2 < number1)
{
temp = number1;
number1 = number2;
number2 = temp;
}

if ( number3 < number2)
{
temp = number2;
number2 = number3;
number3 = temp;
}

if ( number4 < number3)
{
temp = number3;
number3 = number4;
number4 = temp;
}

if ( number5 < number4)
{
temp = number4;
number4 = number5;
number5 = temp;
}
}

System.out.println( number1 + " " + number2 + " " + number3 + " " + number4 + " " + number5);
System.out.println( number5 + " " + number4 + " " + number3 + " " + number2 + " " + number1);

Sorting will get easier as you learn some of the algorithms and how they work. Until then, I hope this helps.

The basic idea is to check each pair and swap the values. You will need to do this multiple times in order to get the last value to the beginning of your list of values.

Sort 4 numbers without array

A nice way to sort a small, fixed size of inputs is using a sorting network.

This network sorts 4 elements:

int tmp;
if (a > b) { tmp = a; a = b; b = tmp; }
if (c > d) { tmp = c; c = d; d = tmp; }
if (a > c) { tmp = a; a = c; c = tmp; }
if (b > d) { tmp = b; b = d; d = tmp; }
if (b > c) { tmp = b; b = c; c = tmp; }

Each line does a comparison and swap between two elements.

If you want to sort in reverse order, just flip the > signs to < signs.

Links:

  • Sorting network - Wikipedia
  • "Introduction to Sorting Networks" - Doug Hoyte
  • List of smallest and fastest sorting networks for a given number of inputs


Related Topics



Leave a reply



Submit